2011-08-06 13 views
0

코어 데이터의 웹 서비스에서 다운로드 한 JSON 데이터를 유지하는 배경 스레드가 있습니다. 이상하고 이상한 경우를 제외하고는 예상대로 작동합니다. 나는 이것이 왜 발생 하는지를 알 수 없었고, 재현 할 수 없다. 이것은 시뮬레이터와 장치 모두에서 발생했기 때문에 메모리와 관련이 없습니다.백그라운드 스레드에서 홀수 코어 데이터가 충돌 함

충돌하는 방법은 다음과 같습니다.

+ (Hotspot *)insertOrUpdateWithDictionary:(NSDictionary *)hotspotJSON error:(NSError **)error { 
NSManagedObjectContext *moc = [[ContentProviderController sharedContentProviderController] managedObjectContext]; 
Hotspot *hotspot = [Hotspot managedObjectWithPrimaryKey:[hotspotJSON objectForKey:@"id"] error:error]; 
if (*error == nil) { 
    if (hotspot) { 
     // Delete hotspot and cascade to other managed objects 
     [moc deleteObject:hotspot]; 
    } 
    hotspot = [NSEntityDescription insertNewObjectForEntityForName:@"Hotspot" inManagedObjectContext:moc]; 
    [hotspot setJSONProperties:hotspotJSON error:error]; 
} 
if (*error == nil) { 
    return hotspot; 
} 
return nil; 

}

managedObjectWithPrimaryKey은 다음과 같다;

+ (Hotspot *)managedObjectWithPrimaryKey:(NSString *)primaryKey error:(NSError **)error { 
if (primaryKey) { 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:NSStringFromClass([self class]) inManagedObjectContext:[[ContentProviderController sharedContentProviderController] managedObjectContext] ]]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(hotspotId like %@)", primaryKey]; 
    [fetchRequest setPredicate:predicate]; 
    [fetchRequest setFetchLimit:1]; 
    NSArray *results = [[[ContentProviderController sharedContentProviderController] managedObjectContext] executeFetchRequest:fetchRequest error:error]; 
    RELEASE_SAFELY(fetchRequest); 
    if (*error == nil && results) { 
     return ([results count] > 0) ? [results objectAtIndex:0] : nil; 
    } else { 
     GTMLoggerError(@"error while retrieving process with hotspotId=%@; %@", primaryKey, [*error description]); 
     return nil; 
    }  
} else { 
    *error = [NSError errorWithDomain:kCoreDataPersistenceError code:1000 userInfo:[NSDictionary dictionaryWithObject:@"Attempt to access an Hotspot with an invalid (null) hotspotId." forKey:NSLocalizedDescriptionKey]]; 
    return nil; 
} 

}

setJSONProperties은 단순히 다음과 같은 관리 객체의 속성을 설정하는 것입니다; 여기

- (void)setJSONProperties:(NSDictionary *)dictionary error:(NSError **)error { 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

// 설정 핫스팟 속성

RELEASE_SAFELY(pool); 

}

다음과 같이 로그가 보이는 충돌;

** Call stack at first throw: 
(
    0 CoreFoundation      0x36b5964f __exceptionPreprocess + 114 
    1 libobjc.A.dylib      0x33db2c5d objc_exception_throw + 24 
    2 CoreFoundation      0x36b58edf __NSFastEnumerationMutationHandler + 214 
    3 libobjc.A.dylib      0x33db936d objc_enumerationMutation + 24 
    4 CoreData       0x36ee5c89 -[NSManagedObjectContext executeFetchRequest:error:] + 1888 
    5 magazine       0x000cd76d +[Page managedObjectWithPrimaryKey:error:] + 288 
    6 magazine       0x000ccc65 -[Hotspot setJSONProperties:error:] + 724 
    7 magazine       0x000cc943 +[Hotspot insertOrUpdateWithDictionary:error:] + 194 
    8 magazine       0x000c3d39 -[ContentProviderController persistHotspots:] + 368 
    9 magazine       0x000c05e5 -[ContentProviderController doPersistenceJobForIssueObjectId:] + 704 
    10 CoreFoundation      0x36b5c7a4 __invoking___ + 68 
    11 CoreFoundation      0x36ad443d -[NSInvocation invoke] + 108 
    12 Foundation       0x34f8043f -[NSInvocationOperation main] + 78 
    13 Foundation       0x34f19d1b -[__NSOperationInternal start] + 658 
    14 Foundation       0x34f19a7f -[NSOperation start] + 22 
    15 Foundation       0x34f7fecb ____startOperations_block_invoke_2 + 46 
    16 libdispatch.dylib     0x339d88e7 _dispatch_call_block_and_release + 10 
    17 libdispatch.dylib     0x339d362d _dispatch_worker_thread2 + 252 
    18 libsystem_c.dylib     0x3483c591 _pthread_wqthread + 264 
    19 libsystem_c.dylib     0x3483cbc4 _init_cpu_capabilities + 4294967295 
) 

나는 완전히 열거하는 동안 개체가 돌연변이되지 않아야 함을 이해하지만 별도의 관리되는 개체 (페이지)이 스레드 (에서 볼 수 있습니다 이유를 내가 이해하지 못하는 것은 - [핫스팟 setJSONProperties : 오류 :] + 724). 각 관리 객체는 순차적으로 유지되므로 "페이지"객체가이 "핫스팟"객체보다 먼저 유지됩니다.

답변

0

좋아요, 그렇기 때문에 범인은 동일한 데이터를 변경하려고하는 두 개의 동시 코어 데이터 스레드를 생성하는 매우 복잡한 이벤트 조합이었습니다.

이 문제를 해결하면 문제가 해결됩니다.

관련 문제