开发者

Core Data: Updating Data Corresponding to a Key

开发者 https://www.devze.com 2023-02-13 03:30 出处:网络
How can I update data corresponding to any开发者_如何学Go key in Core Data? ThanksFor updating any entry in core data you need first fetch that entry then make object for that entity then set all pro

How can I update data corresponding to any开发者_如何学Go key in Core Data?

Thanks


For updating any entry in core data you need first fetch that entry then make object for that entity then set all properties of that see this code,

NSMutableArray *fetchResults;
    NSString *entityName=@"YourEntity";
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:globalManagedObjectContext];
    [fetchRequest setEntity:entity];

    fetchResults = [NSMutableArray arrayWithArray:[globalManagedObjectContext executeFetchRequest:fetchRequest error:nil]];
    [fetchRequest release];

[fetchResults filterUsingPredicate:[NSPredicate predicateWithFormat:@"id==%@",comparing_key]];
    objEntity=[fetchResults objectAtIndex:0];

    objEntity.pro1=@"some value";
    objEntity.pro2=@"some value";

    if (![globalManagedObjectContext save:nil]) 
        NSLog(@"Error in storing to database");
0

精彩评论

暂无评论...
验证码 换一张
取 消