神奇的记录IOSobjective C。我们应该创造什么样的背景?

Magical Record IOS objective C. What context should we create?

您好,我正在使用 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,它是一个直接连接到协调器的私有队列上下文,通常它是你的根上下文。
  2. MR_defaultContext,它是一个主队列上下文,它有 MR_rootSavingContext 作为它的父级,通常它会是你的 UI 上下文,用它来获取和显示你的数据屏幕。

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

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

所以总之,这些语境没有好坏之分,需要根据自己的需求选择合适的。