Convert NSArray to Swift Array

I’m currently using obj.types.allObjects as Type[], but that feels like a hack/workaround. Then revise your feelings. What you’re doing is exactly how you do “type-cast / convert the NSArray to Array<Type>[].” An NSArray always arrives into Swift as containing AnyObject (i.e. an Array<AnyObject>, aka AnyObject[]). Casting it down to a specific type, if you know … Read more

NSPersistentCloudKitContainer: How to check if data is synced to CloudKit

To quote “Framework Engineer” from a similar question in the Apple Developer forums: “This is a fallacy”. In a distributed system, you can’t truly know if “sync is complete”, as another device, which could be online or offline at the moment, could have unsynced changes. That said, here are some techniques you can use to … Read more

How to sync records between Core Data and CloudKit efficiently

As of iOS 13, there are new API’s that simplify this synchronization for developers. I would recommend you to watch the WWDC19 session about the new synchronization between CoreData and CloudKit. Please note that these new API’s only work for iOS 13+. Video: https://developer.apple.com/videos/play/wwdc2019/202/ In short, you need to start using NSPersistentCloudKitContainer instead of NSPersistentContainer. … Read more

Deleting all data in a Core Data entity in Swift 3

You’re thinking of NSBatchDeleteRequest, which was added in iOS 9. Create one like this: let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: “Employee”) let request = NSBatchDeleteRequest(fetchRequest: fetch) You can also add a predicate if you only wanted to delete instances that match the predicate. To run the request: let result = try managedObjectContext.executeRequest(request) Note that batch requests don’t … Read more

What does the Indexed Property of a CoreData attribute do?

I would recommend to read this on indexes: http://en.wikipedia.org/wiki/Index_(database).  Simply put, a database engine creates a new structure which keeps the indexed column (which corresponds to a property) sorted and a link to the corresponding row for each entry (primary key). This allows for faster searches (since search in ordered lists is faster than in … Read more