백그라운드 스레드를 사용하여 UIManagedDocument
/Core Data
의 데이터를 업데이트하는 중 일부 문제가 있습니다. 특히 NSFetchResultsController
을 사용하여 배경 스레드의 지오 코딩 된 데이터를 기반으로지도 주석을 업데이트합니다 (주 MOC로 다시 병합 한 후). UIManagedDocument
이 해당 점포 및/또는 해당 점포로 데이터를 커밋하기 때문에지도가 업데이트되지 않습니다. 여러 MOC (부모 및 자식). 앱을 닫고 다시 열면 주석이 채워지므로 영구 저장소에 대한 커밋이 어느 시점에 발생하지만이 커밋을 강제로 수행하는 방법이 명확하지 않아 NSFetchResultsController
을 업데이트합니다.코어 데이터 : NSManagedObjectContext, NSFetchResultsController 및 UIManagedDocument
상무부 업데이트 배경 스레드 : (내 애플 대리자에서) 그 호출되는 저장하면
- (void) populateGPSCoordsInClubsInContext: (NSManagedObjectContext *) mainCtx
{
dispatch_queue_t MapFetchQ = dispatch_queue_create("Google Map Data Fetcher", NULL);
dispatch_async(MapFetchQ, ^{
NSManagedObjectContext * ctxThread = [[NSManagedObjectContext alloc] init];
[ctxThread setPersistentStoreCoordinator:mainCtx.persistentStoreCoordinator];
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Club"];
request.predicate = [NSPredicate predicateWithFormat:@"inRegion.name=%@", self.name];
NSError *error = nil;
NSArray * clubs = [ctxThread executeFetchRequest:request error:&error];
NSLog(@"[%@] Fetching map data. Club count is %d", self.name, [clubs count]);
int delayCounter = 0;
for(Club * club in clubs)
{
if(![club.hasCoord boolValue] && club != nil)
{
delayCounter++; // to deal with google maps api's DoS protection
[club setLongitudeAndLattitudeFromGoogle];
NSError * error;
if(![ctx save:&error])
NSLog(@"[%@] Problem saving region to database.", self.name);
}
if(delayCounter == 8)
{
[NSThread sleepForTimeInterval:(NSTimeInterval)2.0];
delayCounter = 0;
}
}
});
dispatch_release(MapFetchQ);
}
, 나는 주 스레드의 알림을 잡아 그렇게 같은 :
- (void) contextDidSave: (NSNotification *) notification
{
NSManagedObjectContext * ctx = [self.clubsDB managedObjectContext];
[ctx mergeChangesFromContextDidSaveNotification:notification];
NSArray * updates = [[notification.object updatedObjects] allObjects];
for(Club * club in updates) // This never fires because updates never has objects
{
NSLog(@"*********** %@", club.name);
}
NSLog(@"[%@] %@", [self class], NSStringFromSelector(_cmd));
}
여기에 몇 가지 코드는
-(void) setupFRC
{
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Club"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:@"inRegion.name=%@ AND hasCoord=%@",[self.clubsDB regionTitleAsString], [NSNumber numberWithBool:YES]]; // Follow the relationshop and only display clubs from THIS region.
//request.predicate = [NSPredicate predicateWithFormat:@"inRegion.name=%@",[self.clubsDB regionTitleAsString]];
self.debug = YES;
self.fetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.clubsDB.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}
0 :
내가 같은 내 가져온 결과 컨트롤러를 설정 한 (술어는 데이터 저장소에 커밋 한 후 응용 프로그램을 다시 부팅 예상대로 결과가 정확)
페치 된 결과 컨트롤러가 원하는대로 동작하도록 적절한 MOC를 업데이트하는 방법에 대한 아이디어가 있습니까?
누구나? 완전히 갇혀 .. : / – Andrew