【问题标题】:Magical Record IOS objective C. What context should we create?Magical Record IOS 目标 C. 我们应该创建什么上下文?
【发布时间】:2016-08-01 03:29:12
【问题描述】:

您好,我正在使用 Core Data IOS 为目标 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;
}

非常感谢任何帮助。谢谢

【问题讨论】:

  • 好的,所以对于第一个,最好使用像你正在做的私有队列上下文。对于第二个,最好使用MR_defaultContext,因为我认为您获取的值是用于 UI。
  • 所以我们应该改变这个 NSManagedObjectContext *moc = [NSManagedObjectContext MR_rootSavingContext];到 NSManagedObjectContext *moc = [NSManagedObjectContext MR_defaultContext];几乎所有的 fetch 请求都应该使用 MR_defaultContext?
  • 不是全部抓取,但是如果您抓取的数据是为了在屏幕上显示,那么在MR_defaultContext 中进行,但除了抓取之外不要做任何事情(如插入、更新或删除)在这种情况下。

标签: ios objective-c magicalrecord


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 非常感谢您的详细解释。我将对此进行更深入的研究。至于上面的 2 方法,您建议我们应该使用哪个上下文?我的实施是否合适?
  • 您好,我还有一个关于 Magical 唱片的问题。可以帮忙看看!非常感谢stackoverflow.com/questions/38725256/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多