안녕하세요, 객관적인 C에 대한 마법의 레코드 라이브러리를 사용하여 코어 데이터 IOS와 함께 일하고 있어요. 라이브러리는 많은 NSManageObjectContext 개시가 있습니다. 앱 성능과 사용자 만족도를 유지하기 위해 우리는 무엇을 사용해야합니까?Magical Record IOS 목적 C. 우리는 어떤 상황에서 만들어야합니까?
많은
+ [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 응답이 기능 거래를 성공적으로 요청을 가져
+ (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;
}
어떤 도움이 많이 감사하다를 resonse
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);
});
}
}];
를받을 그리고이 기능을 수행 할 때마다 데이터베이스에 기록을 저장합니다. 당신은 설정에 CoreData 스택 MagicalRecord의 기본 방법을 사용할 때 감사
나는 시작하기 전에, 당신이 알고 이해해야 두 개 더 상황이 있다고 생각
첫 번째 경우에는 수행중인 것과 같은 개인 대기열 컨텍스트를 사용하는 것이 좋습니다. 두 번째 경우, MR_defaultContext를 사용하는 것이 더 낫다. 왜냐하면 내가 가져 오는 값이 UI를위한 것이라고 생각하기 때문이다. –
그래서 우리는 이것을 변경해야합니다. NSManagedObjectContext * moc = [NSManagedObjectContext MR_rootSavingContext]; to NSManagedObjectContext * moc = [NSManagedObjectContext MR_defaultContext]; 거의 모든 가져 오기 요청은 MR_defaultContext를 사용해야합니까? –
가져 오기가 전부는 아니지만 가져 오는 데이터가 화면에 표시되는 것이면 'MR_defaultContext'에서 수행하고이 컨텍스트에서 가져 오기 (예 : 삽입, 업데이트 또는 삭제) 이외의 작업을 수행하지 마십시오. –