3

I implemented an NSTimer which call a simple method :

NSTimer *timer = [NSTimer timerWithTimeInterval:kTimerDelay target:self selector:@selector(synchronise) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

This method builds an NSDictionnary, searches objects in CoreData Context with there ids and enumerates :

- (void)synchronise
{
    NSDictionary *postDict = @{...};
    NSArray *objectIdList = [postDict valueForKeyPath:@"object_items.@allKeys"];
    NSArray *objectList = [P_CoreDataObject MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"id IN %@", objectIdList]];
    MyServiceService *myService = [[MyServiceService alloc] init];
    for (P_CoreDataObject *object in objectList) {
        [myService sendLink:object.link_url success:nil failure:nil];
    }
}

I call MR_findAll on an other object to build the NSDictionnary and it works well, but in some rare cases, it crashs !

Does someone have any idea how to understand this crash ?

The stack trace :

objc_msgSend() selector name: _didReceiveConnectionCacheKey:

Thread 0: 0 libsystem_kernel.dylib 0x000000018417680c pread + 8 1 libsqlite3.dylib 0x0000000184ab0b9c sqlite3_snprintf + 13660 2 libsqlite3.dylib
0x0000000184acfb20 sqlite3_log + 5472 3 libsqlite3.dylib
0x0000000184aceab8 sqlite3_log + 1272 4 libsqlite3.dylib
0x0000000184af223c sqlite3_step + 60112 5 libsqlite3.dylib
0x0000000184af3620 sqlite3_value_text + 3412 6 libsqlite3.dylib
0x0000000184ae5088 sqlite3_step + 6428 7 libsqlite3.dylib
0x0000000184ae3960 sqlite3_step + 500 8 CoreData
0x000000018604fab8 _execute + 144 9 CoreData
0x000000018605f988 -[NSSQLiteConnection fetchResultSet:usingFetchPlan:] + 1644 10 CoreData
0x000000018616150c newFetchedRowsForFetchPlan_MT + 1560 11 CoreData
0x00000001860567e4 -[NSSQLCore objectsForFetchRequest:inContext:] + 536 12 CoreData 0x0000000186056218 -[NSSQLCore executeRequest:withContext:error:] + 596 13 CoreData 0x000000018611d910 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 2620 14 CoreData
0x0000000186125d58 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 172 15 libdispatch.dylib 0x00000001840415f0 _dispatch_client_callout + 12 16 libdispatch.dylib 0x000000018404c89c _dispatch_barrier_sync_f_invoke + 96 17 CoreData
0x00000001861185b0 _perform + 176 18 CoreData
0x0000000186055e70 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 296 19 CoreData
0x0000000186054938 -[NSManagedObjectContext executeFetchRequest:error:] + 532 20 CoreData
0x00000001860f6318 -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 416 21 CoreData 0x00000001860f6a44 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 344 22 CoreData
0x00000001860f8d80 internalBlockToNSManagedObjectContextPerform + 104 23 libdispatch.dylib 0x00000001840415f0 _dispatch_client_callout + 12 24 libdispatch.dylib 0x000000018404c89c _dispatch_barrier_sync_f_invoke + 96 25 CoreData
0x00000001860e86fc _perform + 208 26 CoreData
0x00000001860f6824 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 172 27 CoreData
0x0000000186054938 -[NSManagedObjectContext executeFetchRequest:error:] + 532 28 Youboox
0x0000000100236268 __67+[NSManagedObject(MagicalRecord) MR_executeFetchRequest:inContext:]_block_invoke (NSManagedObject+MagicalRecord.m:54) 29 CoreData
0x00000001860ed080 developerSubmittedBlockToNSManagedObjectContextPerform + 192 30 CoreData 0x00000001860ecf48 -[NSManagedObjectContext performBlockAndWait:] + 216 31 Youboox 0x00000001002361ac +[NSManagedObject(MagicalRecord) MR_executeFetchRequest:inContext:] (NSManagedObject+MagicalRecord.m:50) 32 Youboox
0x00000001002344d4 +[NSManagedObject(MagicalFinders) MR_findAllWithPredicate:inContext:] (NSManagedObject+MagicalFinders.m:76) 33 Youboox
0x0000000100234568 +[NSManagedObject(MagicalFinders) MR_findAllWithPredicate:] (NSManagedObject+MagicalFinders.m:84)

EDIT : The craching thread stack trace

Thread 18 Crashed: 0 libobjc.A.dylib
0x0000000183c6dbc8 objc_msgSend + 8 1 CFNetwork
0x0000000184c3a518 SessionConnectionLoadable::_loaderClientEvent_DidReceiveConnectionCacheKey(HTTPConnectionCacheKey*) + 52 2 CFNetwork 0x0000000184cb9178 ___ZN19URLConnectionLoader36protocolDidReceiveConnectionCacheKeyEP22HTTPConnectionCacheKey_block_invoke + 36 3 libdispatch.dylib 0x0000000184041630 _dispatch_call_block_and_release + 20 4 libdispatch.dylib 0x00000001840415f0 _dispatch_client_callout + 12 5 libdispatch.dylib 0x000000018404d634 _dispatch_queue_drain + 860 6 libdispatch.dylib
0x00000001840450f4 _dispatch_queue_invoke + 460 7 libdispatch.dylib 0x000000018404f504 _dispatch_root_queue_drain + 724 8
libdispatch.dylib 0x000000018404f224 _dispatch_worker_thread3 + 108 9 libsystem_pthread.dylib 0x0000000184255470 _pthread_wqthread + 1088 10 libsystem_pthread.dylib 0x0000000184255020 start_wqthread + 0

vmeyer
  • 1,998
  • 21
  • 23
  • 1
    That's odd. This thread doesn't mention the method implicated in the crash. Are you sure it says that thread 0 is the crashing thread? – Phillip Mills Feb 08 '16 at 15:26
  • Dammit ! You're right ! The thread 18 crached. I update my question. – vmeyer Feb 08 '16 at 15:37
  • 3
    I'm getting this crash also. In my case, it is occurring when my app is performing a background app refresh from application:performFetchWithCompletionHandler: I suspect the problem is that I'm using a foreground URLSession, and instead should be using a background one. Does this match your situation at all? – Paul Buchanan May 12 '16 at 16:05
  • Yes I'm using Background Transfer Service with URLSession, and I use both, background and forground sessions. I just realized that I set a "FinishEventsForBackgroundURLSessionBlock" to every URLSession. Could this cause my crash ? – vmeyer May 13 '16 at 12:46
  • related: https://stackoverflow.com/questions/35387246/how-to-debug-and-solve-a-crash-in-afnetworking – user102008 May 19 '16 at 20:29
  • Hi, having the same issue. https://github.com/AFNetworking/AFNetworking/issues/3530 Is any of you using AFNetworking? Also this link might help https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html – Giorgos Ath Aug 22 '16 at 09:04

0 Answers0