开发者

Core Data: Updating Data Corresponding to a Key

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜