从尚未提交的上下文中获取数据。核心数据

Fetch data from context that is not yet committed. CoreData

我使用 MagicalRecord.

在默认 NSManagedObjectContext 中创建了 NSManagedObject 实体的多个实例(例如汽车:NSManagedObject

我没有保存上下文。有没有办法执行获取请求并获取已处于持久状态的数据 在默认上下文中添加的尚未提交的数据?

据我所知……您可以全部获取,然后查看对象的 objectID 以确定哪个已保存。

id all = [ctx fetch..]; 
id savedOnly = [NSMutableArray array];
for(id o in all) {
    if([[o objectID] isTemporary] == NO) {
        [savedOnly addObject:o];
    }
}

或者更改您的代码以用于上下文——这可能更好 :D


也许使用像这样的谓词:

savedOnly = [ctx fetchWithPredicate:@"... self.objectID.isTemporary=NO"];

可能有用...不知道

如果我们谈论的是相同的托管对象上下文 - 是的,您应该还可以获得那些未提交的对象。这是默认行为。托管对象上下文还包含未提交的对象,即尚未保存在持久存储中的对象。

是的,已提取。请在 https://developer.apple.com/reference/coredata/nsmanagedobjectcontext:

查看 Apple 文档

An object that meets the criteria specified by request (it is an instance of the entity specified by the request, and it matches the request’s predicate if there is one) and that has been inserted into a context but which is not yet saved to a persistent store, is retrieved if the fetch request is executed on that context.