I’m slowly beginning to like Core Data

Core Data is by far the worst framework I have ever had to work with. Or is it?

It has a steep learning curve. Though once you’ve got your head around it, it doesn’t seem so bad after all.

I’m really starting to get accustomed to it. There are just some principles one needs to know in order to really not hate it.

I use the MagicalRecord library on top of it, and the mogenerator tool to make everything manageable.

I’ve got multi-threaded code and it cooperates (so far).

My approach is to keep most update operations off of the main thread, and saving that background context will update the main thread context, which in turn updates all the NSFetchedResultsControllers, and now my table views just animate those changes nicely!

I think, after numerous attempts, I am finally getting to be fit with CoreData and now can use it as my main persistence framework from now on. (Until they scrap it and move for something more Swift-style…)

Advertisements

Taking the pain out of CoreData

So far, if I have to make an App that uses CoreData, I will without question be using mogenerator and MagicalRecord.

Here are some links for mogenerator:

https://github.com/rentzsch/mogenerator

And then for MagicalRecord:

http://yannickloriot.com/2012/03/magicalrecord-how-to-make-programming-with-core-data-pleasant/

http://ablfx.com/blog/article/2

https://github.com/magicalpanda/MagicalRecord

They’re worth reading and checking out. CoreData does not seem like such a pain anymore.

Just remember:
* One NSManagedObjectContext per thread. So, if you are doing asynchronous work, save your context to the store on that other thread, then in your main thread callback, make sure you’ve passed an array of objectId’s (NSNumbers) so you can fetch those objects on the main thread.

I’m assuming you’re using CoreData for fairly simple purposes in mobile apps. Not really talking about editing apps with undo managers, etc. Just basic persistence and fetching and so on.

Compound Predicates – “get me objects with these specific ID’s!”

So, I’m hoping you know about Predicates and how they make finding or filtering objects really easy. If not, head over to NSHipster and read up on that.

I had a particular use case, where I had an array of objectID’s (NSNumber objects) that I needed to fetch from my CoreData context.

So the question of “how do I get an array of objects with specific, known, object id’s?” The Answer? Compound Predicates.

The Recipe:

// I use MagicalRecord for CoreData.  Get your object context how you normally would then...
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];  
        
NSMutableArray *predicates = [NSMutableArray array];
for (NSNumber *knownId in knownObjectIds) {
  
  // assuming your object has the property myObjectId
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myObjectId == %@", knownId]; 
  [predicates addObject: predicate];  
}
              
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
                
// like I say, I'm thrilled with MagicalRecord.  
//  This is where you would use this for your fetch request in CoreData
NSArray *fetchedResults = [HSCoreDataObject MR_findAllWithPredicate:compoundPredicate inContext:context];