How to delete an old/unused Data Model Version in Xcode

It’s a hack, but this worked for me: Set the Current version of the model in Xcode to one that you want to keep Remove the .xcdatamodeld from your project (Right-click -> Delete -> Remove Reference Only) Show the contents of the .xcdatamodeld package in the Finder (Right-click -> Show Package Contents) Delete the .xcdatamodel … Read more

Best practice? – Array/Dictionary as a Core Data Entity Attribute [closed]

There is no “native” array or dictionary type in Core Data. You can store an NSArray or an NSDictionary as a transformable attribute. This will use the NSCoding to serialize the array or dictionary to an NSData attribute (and appropriately deserialize it upon access). The advantage of this approach is that it’s easy. The downside … Read more

Cocoa Core Data efficient way to count entities

I don’t know whether using NSFetchedResultsController is the most efficient way to accomplish your goal (but it may be). The explicit code to get the count of entity instances is below: // assuming NSManagedObjectContext *moc NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:moc]]; [request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities) … Read more

Delete/Reset all entries in Core Data?

You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method. NSPersistentStore *store = …; NSError *error; NSURL *storeURL = store.URL; NSPersistentStoreCoordinator *storeCoordinator = …; [storeCoordinator removePersistentStore:store error:&error]; [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; Then, just add the persistent store back to ensure it is recreated properly. The programmatic way for iterating through each entity is both … Read more

How to Sync iPhone Core Data with web server, and then push to other devices? [closed]

I’ve done something similar to what you’re trying to do. Let me tell you what I’ve learned and how I did it. I assume you have a one-to-one relationship between your Core Data object and the model (or db schema) on the server. You simply want to keep the server contents in sync with the … Read more

Core Data vs SQLite 3 [closed]

Although Core Data is a descendant of Apple’s Enterprise Object Framework, an object-relational mapper (ORM) that was/is tightly tied to a relational backend, Core Data is not an ORM. It is, in fact, an object graph management framework. It manages a potentially very large graph of object instances, allowing an app to work with a … Read more

Core Data: Quickest way to delete all instances of an entity

iOS 9 and later: iOS 9 added a new class called NSBatchDeleteRequest that allows you to easily delete objects matching a predicate without having to load them all in to memory. Here’s how you’d use it: Swift 5 let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: “Car”) let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { try myPersistentStoreCoordinator.execute(deleteRequest, with: myContext) … Read more