gpt4 book ai didi

cocoa-touch - 需要对刷新对象 :mergeChanges:YES 进行一些说明

转载 作者:行者123 更新时间:2023-12-03 06:16:10 25 4
gpt4 key购买 nike

一些背景:

我正在尝试在本地数据库中存储大量数据,并且希望尽可能高效地执行此操作。

场景:

有许多相互关联的实体,例如地址与联系人相关联,如下所示:

address <<-> contact

为了管理关系,我在 NSManagedObject 的每个子类中编写了一个方法,下面是一些代码片段:

// class Contact
- (void)manageRelationships
{
@autoreleasepool {
LocalDBManager *localDBManager = [[LocalDBManager alloc] init];

// managing relationships

// map associated addresses

NSPredicate *addressIdPredicate = [NSPredicate predicateWithFormat:@"%K == %@",ADDRESSID,self.addressid];

// below method returns an object as fault by firing a fetch request against context
NSSet *retrievedAddresses = [localDBManager retrieveManagedObjectsForEntity:ADDR_ENTITY withPredicate:addressIdPredicate asFault:YES withPropertyValues:NO error:nil];
self.addresses = retrievedAddresses;


// managing few more relationships
}

}

考虑要点:

由于一个对象可以有多个关系,所以我知道当我映射关系时,内存消耗会增加。

问题:

一旦关系被映射,我想将对象恢复为故障,而不丢失所做的任何更改。

从苹果文档和一些谷歌搜索中,我知道我可以使用 refreshObject:mergeChanges: 方法。因此,我计划在 ManageRelationships 方法中的代码块末尾添加以下行:

[[self managedObjectContext] refreshObject:self mergeChanges:YES];

我有点困惑,想知道 -

Does it mean that whatever changes were made to the object will be stored in persistent store and then the object will turn to a fault? If yes, then can I consider it equivalent to save method of NSManagedObjectContext

请提出建议。

最佳答案

首先,你不需要自己管理关系。让 CoreData 处理这个问题。

Core Data automatically resolves (fires) the fault when you access data in the fault. This lazy loading of the related objects is much better for memory use, and much faster for fetching objects related to rarely used (or very large) objects. CoreData performance

您可以通过简单地实例化联系人地址之间的一对多关系来实现此目的。

关于refreshObject:mergeChanges:你错了。它不等同于 save: 方法。如果您将合并更改设置为 YES,则仅意味着:

If flag is YES, then object’s property values are reloaded from the values from the store or the last cached state then any changes that were made (in the local context) are re-applied over those (now newly updated) values. Cocoa touch Doc

因此,如果您对托管对象 A 进行了一些更改,然后执行了[context refreshObject:A mergeChanges:YES],则对象 A 仍将保持未保存状态。

关于cocoa-touch - 需要对刷新对象 :mergeChanges:YES 进行一些说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14868498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com