Where to place the “Core Data Stack” in a Cocoa/Cocoa Touch application

Summary: There is no need to create a singleton to manage the Core Data stack; indeed doing so is likely to be counter-productive. The Core Data stack happens to be created by the application delegate. Importantly, however, as all the examples show, the stack (principally the managed object context) is not retrieved directly from the … Read more

Core Data and iOS 7: Different behavior of persistent store

Yes, Apple have changed the default journal mode to WAL for iOS7. You can specify the journal mode by adding the NSSQLitePragmasOption to the options when calling addPersistentStoreWithType:configuration:url:options:error. E.g. to set the previous default mode of DELETE: NSDictionary *options = @{ NSSQLitePragmasOption : @{@”journal_mode” : @”DELETE”} }; In my experience WAL gives better performance, but … Read more

Core Data multi thread application

The Apple Concurrency with Core Data documentation is the place to start. Read it really carefully… I was bitten many times by my misunderstandings! Basic rules are: Use one NSPersistentStoreCoordinator per program. You don’t need them per thread. Create one NSManagedObjectContext per thread. Never pass an NSManagedObject on a thread to the other thread. Instead, … Read more

What is NSManagedObjectContext’s performBlock: used for?

The methods performBlock: and performBlockAndWait: are used to send messages to your NSManagedObjectContext instance if the MOC was initialized using NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType. If you do anything with one of these context types, such as setting the persistent store or saving changes, you do it in a block. performBlock: will add the block to the … Read more

How to implement re-ordering of CoreData records?

FetchedResultsController and its delegate are not meant to be used for user-driven model changes. See the Apple reference doc. Look for User-Driven Updates part. So if you look for some magical, one-line way, there’s not such, sadly. What you need to do is make updates in this method: – (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath … Read more

Core Data: NSPredicate for many-to-many relationship. (“to-many key not allowed here”)

You’re trying to compare a collection (categories.name) to a scalar value (category.name). You need to either use a collection comparator (CONTAINS), or use a predicate modifier (ANY/ALL/SOME, etc). Try using: [NSPredicate predicateWithFormat:@”ANY categories.name =[cd] %@”, category.name]; Or: [NSPredicate predicateWithFormat:@”categories.name CONTAINS[cd] %@”, category.name];

NSFetchedResultsController with sections created by first letter of a string

Dave DeLong’s approach is good, at least in my case, as long as you omit a couple of things. Here’s how it’s working for me: Add a new optional string attribute to the entity called “lastNameInitial” (or something to that effect). Make this property transient. This means that Core Data won’t bother saving it into … Read more

Custom setter methods in Core-Data

According to the documentation, it’d be: – (void) setFoo:(NSObject *)inFoo { [self willChangeValueForKey:@”foo”]; [self setPrimitiveValue:inFoo forKey:@”foo”]; [self didChangeValueForKey:@”foo”]; } This is, of course, ignoring the fact that NSManagedObjects only want NSNumbers, NSDates, NSDatas, and NSStrings as attributes. However, this might not be the best approach. Since you want something to happen when the value of … Read more

iOS: Delete ALL Core Data Swift

Try this simple solution: func deleteAllData(entity: String) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let managedContext = appDelegate.managedObjectContext let fetchRequest = NSFetchRequest(entityName: entity) fetchRequest.returnsObjectsAsFaults = false do { let results = try managedContext.executeFetchRequest(fetchRequest) for managedObject in results { let managedObjectData:NSManagedObject = managedObject as! NSManagedObject managedContext.deleteObject(managedObjectData) } } catch let error as NSError { print(“Detele all … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)