[iOS SDK] Core Data with Xcode8, iOS10 and Swift3
There are lots of posting and blog about core data. But there were many changes in core data in Xcode 8, iOS 10 and Swift 3. Actually most postings were not useful for me, so I tried many way to find working codes.
- Data Model
In this example, the model is very simple. Only one Entity with some attributes and there is no relationships.
- Fetch LibraryItem Entityfunc getContext() -> NSManagedObjectContext {return self.persistentContainer.viewContext;}func fetchLibraryList() -> [LibraryItem]? {// context = self.persistentContainer.viewContextlet context = getContext();let fetchRequest : NSFetchRequest<NSFetchRequestResult> = LibraryItem.fetchRequest();let entityDescription = NSEntityDescription.entity(forEntityName: "LibraryItem", in: context);fetchRequest.entity = entityDescription;fetchRequest.predicate = NSPredicate(format: "checkin_date = nil");var fetchedObjects : [LibraryItem]? = nil;do {fetchedObjects = try context.fetch(fetchRequest) as? [LibraryItem];}catch {print("Error in fetching data from core data");}return fetchedObjects;}
No comments:
Post a Comment