gpt4 book ai didi

ios - Magical Record IOS objective C. 我们应该创建什么样的上下文?

转载 作者:行者123 更新时间:2023-11-28 19:36:08 25 4
gpt4 key购买 nike

您好,我正在使用 Core Data IOS 为 Objective-C 使用神奇的记录库。该库有许多 NSManageObjectContext 启动。我们应该使用什么来保持应用性能和良好的用户体验?

有很多

+ [NSManagedObjectContext MR_newContext]: Sets the default context as it's parent context. Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newMainQueueContext]: Has a concurrency type of NSMainQueueConcurrencyType.
+ [NSManagedObjectContext MR_newPrivateQueueContext]: Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newContextWithParent:…]: Allows you to specify the parent context that will be set. Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newContextWithStoreCoordinator:…]: Allows you to specify the persistent store coordinator for the new context. Has a concurrency type of NSPrivateQueueConcurrencyType.

什么上下文启动是好的?

例如这个函数处理JSON响应并在成功接收到响应时将记录保存到数据库

NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];


[Stamp MR_truncateAllInContext:localContext];

[responseJSON[@"root"] enumerateObjectsUsingBlock:^(id attributes, NSUInteger idx, BOOL *stop) {
Stamp *stamp = [Stamp MR_createEntityInContext:localContext];
[stamp setOrderingValue:idx];
[stamp updateWithApiRepresentation:attributes];
}];

[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(!error, error);
});
}
}];

这个函数执行获取请求

+ (NSArray *)yearsDropDownValues
{
NSManagedObjectContext *moc = [NSManagedObjectContext MR_rootSavingContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [Stamp entityInManagedObjectContext:moc];
request.entity = entity;
request.propertiesToFetch = @[StampAttributes.year];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
request.sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:StampAttributes.year ascending:NO]];

NSArray *years = [moc executeFetchRequest:request error:nil];

NSMutableArray *res = [NSMutableArray array];
for (NSDictionary *year in years) {
[res addObject:@{@"code": [NSString stringWithFormat:@"%@ Collections", year[@"year"]], @"value": year[@"year"] }];
}

return res;
}

非常感谢任何帮助。谢谢

最佳答案

在我开始之前,我认为还有两个上下文你​​应该知道和理解,当你使用 MagicalRecord 的默认方式设置你的 CoreData 堆栈时,它们是自动创建的:

  1. MR_rootSavingContext,它是一个直接连接到协调器的私有(private)队列上下文,通常它是您的根上下文。
  2. MR_defaultContext,它是一个主队列上下文,它有 MR_rootSavingContext 作为它的父级,通常它是你的 UI 上下文,用它来获取和显示你的数据屏幕。

现在我将这五个上下文一一解释:

  1. MR_newContext,一个新的私有(private)队列上下文,它有 MR_defaultContext 作为它的父上下文。它等同于调用 [NSManagedObjectContext MR_newContextWithParent:[NSManagedObjectContext ME_defaultContext]]。这种类型的上下文适用于繁重的批处理操作,例如插入、更新、删除数百个对象。由于所有操作都在后台线程上运行,因此不会阻塞 UI。然而缺点是,它带来了额外的复杂性,尤其是当您有多个这样的上下文时,在保存这些上下文时可能会发生冲突。
  2. MR_newMainQueueContext,一个没有父上下文的新主队列上下文。它是 MR_rootSavingContext 的兄弟,因为它们连接到相同的 NSPersistentStoreCoordinator。您在此类上下文中执行的所有操作都会阻塞 UI,因此请勿在此上下文中执行任何繁重的工作。
  3. MR_newPrivateQueueContext,类似于MR_newContext,但没有父上下文。它是 MR_rootSavingContext 的兄弟。
  4. MR_newContextWithParent,一种创建私有(private)队列上下文以及指定父上下文的简便方法。
  5. MR_newContextWithStoreCoordinator,一个新的私有(private)队列上下文,它使用指定的 NSPersistentStoreCoordinator。在我看来,只有知道如何正确使用具有不同协调器的上下文,才不要使用它。

总之,这些上下文没有好坏之分,需要根据自己的需求选择合适的。

关于ios - Magical Record IOS objective C. 我们应该创建什么样的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690585/

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