swiftui how to fetch core data values from Detail to Edit views

Here is a simplified version of your code Just paste this code into your project and call YourAppParent() in a body somewhere in your app as high up as possible since it creates the container. import SwiftUI import CoreData //Class to hold all the Persistence methods class CoreDataPersistence: ObservableObject{ //Use preview context in canvas/preview let … Read more

NSPredicate: Combine CONTAINS with IN

I don’t think that you can combine “IN” with “CONTAINS” in a predicate. But you could split the search string into words, and create a “compound predicate”: NSString *searchString = @”John Sm “; NSArray *words = [searchString componentsSeparatedByString:@” “]; NSMutableArray *predicateList = [NSMutableArray array]; for (NSString *word in words) { if ([word length] > 0) … Read more

Multiple NSEntityDescriptions Claim NSManagedObject Subclass

Post-automatic-caching This should not happen anymore with NSPersistent[CloudKit]Container(name: String), since it seems to cache the model automatically now (Swift 5.1, Xcode11, iOS13/MacOS10.15). Pre-automatic-caching NSPersistentContainer/NSPersistentCloudKitContainer does have two constructors: init(name: String) init(name: String, managedObjectModel model: NSManagedObjectModel) The first is just a convenience initializer calling the second with a model loaded from disk. The trouble is that … Read more

How to disable WAL journal mode

To disable WAL mode, set the journal_mode to DELETE NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary]; [pragmaOptions setObject:@”DELETE” forKey:@”journal_mode”]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, pragmaOptions, NSSQLitePragmasOption, nil]; [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]

How to create a Core Data predicate to test that a relation contains all given objects?

The following predicate could work: [NSPredicate predicateWithFormat:@”([email protected] == %d) AND (SUBQUERY(items, $x, $x IN %@)[email protected] == %d)”, itemSet.count, itemSet, itemSet.count]; The predicate checks first that the number of items is equal to the size of the given itemSet, and then checks that the number of items which are member of itemSet is also equal to … Read more

Xcode 8 generates broken NSManagedObject subclasses for iOS 10

I finally got mine to work. Here is what I did. (Flights is one of my entities) I setup the xcdatamodeld as follows And then the entity as Then I used Editor -> Create NSManagedObject Subclass This creates two files for my flights entity Flights+CoreDataProperties.swift Flights+CoreDataClass.swift I renamed Flights+CoreDataClass.swift to Flights.swift Flights.swift is just import … Read more

What do I have to do to get Core Data to automatically migrate models?

I’ve now found out that this is quite simple – once you know where to look. In my AppDelegate I set-up the NSPersistentStoreCoordinator – and you need to add some options to this to tell it to handle auto-migrate: NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; NSError *error; _persistentStoreCoordinator = … Read more

NSFetchedResultsController with predicate ignores changes merged from different NSManagedObjectContext

Turns out main NSManagedObjectContext didn’ t event fire NSManagedObjectContextObjectsDidChangeNotification for updated objects because it is not done for faulted objects. Generic fix (or keep a track of object IDs that needs this treatment): NSManagedObjectContext *context = [self managedObjectContext]; for(NSManagedObject *object in [[notification userInfo] objectForKey:NSUpdatedObjectsKey]) { [[context objectWithID:[object objectID]] willAccessValueForKey:nil]; } [context mergeChangesFromContextDidSaveNotification:notification]; From NSManagedObject Class … Read more