How to update @FetchRequest, when a related Entity changes in SwiftUI?

I also struggled with this and found a very nice and clean solution: You have to wrap the row in a separate view and use @ObservedObject in that row view on the entity. Here’s my code: WineList: struct WineList: View { @FetchRequest(entity: Wine.entity(), sortDescriptors: [ NSSortDescriptor(keyPath: \, ascending: true) ] ) var wines: FetchedResults<Wine> var … Read more

Core Data NSPredicate with to-Many Relationship

Core Data predicates with “NOT ANY” do not work (that seem to be a Core Data bug). Actually [NSPredicate predicateWithFormat:@”NOT(ANY couponOwners.userId = %@)”, @”4″]; returns the same result set as [NSPredicate predicateWithFormat:@”ANY couponOwners.userId != %@”, @”4″]; which is of course wrong. As a workaround, you can use a SUBQUERY: [NSPredicate predicateWithFormat:@”SUBQUERY(couponOwners, $c, $c.userId == %@).@count … Read more

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]