Ios 参考NSArray更新核心数据中的现有对象
我获取了核心数据实体中的对象。然后我与NSArray进行交叉检查,找出数据库中现有的对象是什么。 然后我将新对象插入数据库。我正在努力学习如何更新代码中的现有对象。请帮我以最好的方式做这件事Ios 参考NSArray更新核心数据中的现有对象,ios,objective-c,core-data,Ios,Objective C,Core Data,我获取了核心数据实体中的对象。然后我与NSArray进行交叉检查,找出数据库中现有的对象是什么。 然后我将新对象插入数据库。我正在努力学习如何更新代码中的现有对象。请帮我以最好的方式做这件事 // get manageObjectContext AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appD
// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSArray *arrIds = [arr valueForKeyPath:@"anId"];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SomeEntity"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"anId", arrIds];
NSArray *existingItems = [context executeFetchRequest:fetchRequest error:&error];
//Get the existing ids:
NSArray *existingIds = [existingItems valueForKeyPath:@"anId"];
for (NSDictionary *item in arr) {
if ([existingItems containsObject:topic[@"anId"]]) {
// update exsising item
}
else{
// Insert new items done .........
}
你可以使用这个代码 AppDelegate*AppDelegate= [[UIApplication sharedApplication]委托]
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Qr_code"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(user_ID=%@)",
kText];
[request setPredicate:pred];
matches = nil;
NSError *error;
updatedata = [context executeFetchRequest:request
error:&error];
if ([updatedata count] == 0) {
} else {
matches=updatedata[0];
}
你可以使用这个代码 AppDelegate*AppDelegate= [[UIApplication sharedApplication]委托]
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Qr_code"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(user_ID=%@)",
kText];
[request setPredicate:pred];
matches = nil;
NSError *error;
updatedata = [context executeFetchRequest:request
error:&error];
if ([updatedata count] == 0) {
} else {
matches=updatedata[0];
}