Fastest way to check if an object exists in Core Data or not?

Setup a Core Data request and, instead of actually issuing the query, do the following: NSError *error = nil; NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error]; if (!error) { return count; } else { return 0; } In practice, the method countForFetchRequest:error: returns the number of objects a given fetch request would have returned if it … Read more

How to update existing object in Core Data?

Updating is simple as creating a new one. To update a specific object you need to set up a NSFetchRequest. This class is equivalent to a SELECT statetement in SQL language. Here a simple example: NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@”Favorits” inManagedObjectContext:moc]]; NSError *error = nil; NSArray *results = [moc executeFetchRequest:request error:&error]; … Read more

Swift 3 Core Data Delete Object

The result of a fetch is an array of managed objects, in your case [Event], so you can enumerate the array and delete all matching objects. Example (using try? instead of try! to avoid a crash in the case of a fetch error): if let result = try? context.fetch(fetchRequest) { for object in result { … Read more

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: \Wine.name, ascending: true) ] ) var wines: FetchedResults<Wine> var … Read more

How can I duplicate, or copy a Core Data Managed Object?

Here’s a class I created to perform a “deep copy” of managed objects: attributes and relationships. Note that this does not check against loops in the object graph. (Thanks Jaanus for the starting off point…) @interface ManagedObjectCloner : NSObject { } +(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context; @end @implementation ManagedObjectCloner +(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{ … 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

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