核心数据获取实际存储在 persistentStore 中的数据不是实体的所有对象(persistentStored + 最新创建的对象)
Core data fetch data which actually stored in persistentStore not all objects of entities (persistentStored + latest created obj)
我有一个实体名称是 student,我想获取住在 Pune 市的学生数据 (city == pune) .在 table 中,有 2 名学生住在 pune city。
但在此之前,我创建了 也住在浦那 市的新学生对象。
因此,当我对学生实体发出获取请求时,它会给我 3 个学生数据,其中还包括我当前的学生数据(2 个来自 table 和 1 个我刚刚创建的未保存在 table)
所以我的问题是我只想要那些我实际上
存储在持久性数据中而不是刚刚创建的数据。
Entity data (student)
--------------------------------------------------------
student name city class
--------------------------------------------------------
Atul pune 7
Sagar mumbai 7
Ganesh pune 7
--------------------------------------------------------
当前对象
Student * obj = [[Mlov alloc] initWithEntity:[NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
obj.name = @"Nitin";
obj.city = @"pune";
obj.class = 10;
NSError * error = nil;
NSPredicate * pred = [NSPredicate predicateWithFormat:@"city == 'pune'"];
NSFetchRequest * req = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
[req setReturnsObjectsAsFaults:NO];
[req setPredicate:pred];
NSError * error = nil;
NSArray * result =[context executeFetchRequest:req error:nil];
for (Student * obj in result)
NSLog(@"%@",obj.name);
Expected output as only 2 students data (Atul and Ganesh)
Actually giving output as 3 students data (Atual, Ganesh and Nitin)
我需要获取存储在持久性存储中的那些学生数据,如果您有任何建议,请告诉我。
提前致谢。
解决方案 1
您可以过滤掉出现在上下文的 insertedObjects 中的对象 属性。
[result filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT SELF IN %@",
context.insertedObjects]];
解决方案 2
为查询使用新的上下文。
我有一个实体名称是 student,我想获取住在 Pune 市的学生数据 (city == pune) .在 table 中,有 2 名学生住在 pune city。
但在此之前,我创建了 也住在浦那 市的新学生对象。
因此,当我对学生实体发出获取请求时,它会给我 3 个学生数据,其中还包括我当前的学生数据(2 个来自 table 和 1 个我刚刚创建的未保存在 table)
所以我的问题是我只想要那些我实际上 存储在持久性数据中而不是刚刚创建的数据。
Entity data (student)
--------------------------------------------------------
student name city class
--------------------------------------------------------
Atul pune 7
Sagar mumbai 7
Ganesh pune 7
--------------------------------------------------------
当前对象
Student * obj = [[Mlov alloc] initWithEntity:[NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
obj.name = @"Nitin";
obj.city = @"pune";
obj.class = 10;
NSError * error = nil;
NSPredicate * pred = [NSPredicate predicateWithFormat:@"city == 'pune'"];
NSFetchRequest * req = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
[req setReturnsObjectsAsFaults:NO];
[req setPredicate:pred];
NSError * error = nil;
NSArray * result =[context executeFetchRequest:req error:nil];
for (Student * obj in result)
NSLog(@"%@",obj.name);
Expected output as only 2 students data (Atul and Ganesh)
Actually giving output as 3 students data (Atual, Ganesh and Nitin)
我需要获取存储在持久性存储中的那些学生数据,如果您有任何建议,请告诉我。 提前致谢。
解决方案 1
您可以过滤掉出现在上下文的 insertedObjects 中的对象 属性。
[result filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT SELF IN %@",
context.insertedObjects]];
解决方案 2
为查询使用新的上下文。