PJC
PJC

Reputation: 997

Core Data: performance issue. Instruments's output is cryptic

I am using Core Data and RestKit in a basic way (just the default RestKit setup).

From time to time, the UI gets stuck for a very long time (50s on an iPhone 5). There can be quite a few objects, but nothing crazy and objects are small (no images etc).

I suspect it is due to some context saving, but I am not sure: never in my app do I save things manually.

Instruments' Time Profiler clearly shows what gets stuck, but I don't understand the output nor what's causing this. The Heaviest Stack Trace looks like this:

14  57129.0  Main Thread  0x4ea79 :0
13 CoreData 56680.0  -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:]
12 CoreData 56523.0  -[NSManagedObjectContext executeFetchRequest:error:]
11 CoreData 56462.0  -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]
10 CoreData 56442.0  -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:]
 9 CoreData 56291.0  -[NSManagedObjectContext executeFetchRequest:error:]
 8 CoreData 50210.0  -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
 7 CoreData 50193.0  -[NSMappedObjectStore executeFetchRequest:withContext:]
 6 CoreData 49978.0  -[NSDictionaryStoreMap handleFetchRequest:]
 5 Foundation 36516.0  -[NSPredicate evaluateWithObject:]
 4 Foundation 35865.0  -[NSComparisonPredicate evaluateWithObject:substitutionVariables:]
 3 Foundation 16845.0  -[NSFunctionExpression expressionValueWithObject:context:]
 2 CoreData 11823.0  -[NSDictionaryMapNode valueForKey:]
 1 CoreFoundation 1225.0  -[__NSCFString isEqualToString:]
 0 libobjc.A.dylib 36.0  -[NSObject class]

Can someone point me in the right direction?

Upvotes: 0

Views: 167

Answers (1)

Rob T
Rob T

Reputation: 147

Have you tried setting your managed object cache to RKInMemoryManagedObjectCache during core data setup? I tried this after seeing this issue on github: https://github.com/RestKit/RestKit/issues/1232 and it significantly sped up my restkit/core data benchmarks. Here is what the code looks like:

objectManager.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext: managedObjectStore.persistentStoreManagedObjectContext];

From the RestKit docs at http://restkit.org/api/0.20.0/Classes/RKInMemoryManagedObjectCache.html: "(RKInMemoryManagedObjectCache) Provides a fast managed object cache where-in object instances are retained in memory to avoid hitting the Core Data persistent store. Performance is greatly increased over fetch request based strategy at the expense of memory consumption."

Upvotes: 1

Related Questions