首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 7 CoreData中的大容量更新数据

iOS 7 CoreData中的大容量更新数据
EN

Stack Overflow用户
提问于 2014-07-25 12:20:32
回答 2查看 1.5K关注 0票数 1

是否有办法通过一个SQL-查询运行某个实体的所有对象的更新?不是去取和跑-循环。

例如,喜欢运行

代码语言:javascript
复制
UPDATE someEntity SET filed1 = value1 WHERE field2 = value2
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-20 05:50:19

Mercelo的答案在iOS-7中是行不通的,你可以

代码语言:javascript
复制
NSFetchRequest *fetchAllRSSItems = [NSFetchRequest fetchRequestWithEntityName:[RSSItem entityName]];
NSError *fetchError;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchAllRSSItems error:&fetchError];

if (results == nil) {

    NSLog(@"Error: %@", [fetchError localizedDescription]);
} else {

    for (RSSItem *rssItem in results) {
        rssItem.read = [NSNumber numberWithBool:YES];
    }

    [self saveManagedObjectContext];
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-25 12:35:59

在iOS 8上引入了核心数据批处理更新:

代码语言:javascript
复制
NSBatchUpdateRequest *batchRequest = [NSBatchUpdateRequest batchUpdateRequestWithEntityName:   [RSSItem entityName]];
batchRequest.propertiesToUpdate = @{NSStringFromSelector(@selector(read)): @YES};
batchRequest.resultType = NSStatusOnlyResultType; // NSStatusOnlyResultType is the default
batchRequest.affectedStores = @[...]; // Optional, stores this request should be sent to
batchRequest.predicate = [NSPredicate predicateWithFormat:@"..."]; // Optional, same type of predicate you use on NSFetchRequest

NSError *requestError;
NSBatchUpdateResult *result = (NSBatchUpdateResult *)[self.managedObjectContext executeRequest:batchRequest error:&requestError];

if (result) {
    // Batch update succeeded
} else {
    NSLog(@"Error: %@", [requestError localizedDescription]);
}

但是,它不更改上下文:它直接更改持久存储。这意味着不需要进行验证,之后需要更新UI。

这个答案是基于马修·莫雷的这篇文章的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24955709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档