fetching from CloudKit and saving to CoreData - ios

this is going to be a really noob question but I'm trying to get my app to download data from CloudKit and then save it to CoreData.
When I run this type of code, I get the following error. I'm really noob at CoreData, so this has been hard for me to understand. I think it has something to do with the way I'm dispatching the requests, but I'm not sure how I'm supposed to fix it. The error I get is:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording
'
Anyone have any ideas?
import UIKit
import CloudKit
import CoreData
class Start: UIViewController {
var classroomEN: String?
var classroomTC: String?
var classroomSC: String?
var videos = [NSManagedObject]()
override func viewDidLoad() {
fetchData()
fetchDataTC()
}
func fetchData() {
//added to fetch data from CloudKit
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let queryEN = CKQuery(recordType: "ClassroomFAQEN", predicate: predicate)
let queryTC = CKQuery(recordType: "ClassroomFAQTC", predicate: predicate)
queryEN.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
queryTC.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
publicData.performQuery(queryEN, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
for entry in results! {
let newFAQ = classFAQ()
newFAQ.title = entry["Title"] as! String
newFAQ.content = entry["Content"] as! String
if entry["Picture"] != nil {
print("There is no picture")
newFAQ.picture = entry["Picture"] as! String
}
if entry["Video"] != nil {
print("There is no video")
newFAQ.video = entry["Video"] as! String
}
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("ClassroomFAQEN", inManagedObjectContext:managedContext)
let video = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
video.setValue(newFAQ.title, forKey: "title")
video.setValue(newFAQ.content, forKey: "content")
video.setValue(newFAQ.picture, forKey: "picture")
video.setValue(newFAQ.video, forKey: "video")
do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("Reloading data in tableView")
self.fetchDataTC()
})
}
}
else {
print(error)
}
}
}
func fetchDataTC() {
//added to fetch data from CloudKit
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let queryEN = CKQuery(recordType: "ClassroomFAQEN", predicate: predicate)
let queryTC = CKQuery(recordType: "ClassroomFAQTC", predicate: predicate)
queryEN.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
queryTC.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
publicData.performQuery(queryTC, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
for entry in results! {
let newFAQ = classFAQ()
newFAQ.title = entry["Title"] as! String
newFAQ.content = entry["Content"] as! String
if entry["Picture"] != nil {
print("There is no picture")
newFAQ.picture = entry["Picture"] as! String
}
if entry["Video"] != nil {
print("There is no video")
newFAQ.video = entry["Video"] as! String
}
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("ClassroomFAQTC", inManagedObjectContext:managedContext)
let video = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
video.setValue(newFAQ.title, forKey: "title")
video.setValue(newFAQ.content, forKey: "content")
video.setValue(newFAQ.picture, forKey: "picture")
video.setValue(newFAQ.video, forKey: "video")
do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("Reloading data in tableView")
})
}
}
else {
print(error)
}
}
}

You can use isMainThread to determine if you're on a background thread.. or you can directly write the code like this, which will always ensure it's in main thread:-
dispatch_async(dispatch_get_main_queue(), { () -> Void in
do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
})

Related

Core Data Images not persisting?

I am storing and creating posts with this function in my view controller:
#objc func createPost(){
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
let post = Post(context: managedContext)
var mediaURI: URL?
let timeInSeconds = Int64((postDate?.timeIntervalSince1970)!)
if isVideo == true {
let filename = String(timeInSeconds) + ".MOV"
FileService.uploadVideo(videoURL: pickedVideoURL, name: filename)
post.mediaFilename = filename
} else {
let filename = String(timeInSeconds) + ".JPG"
FileService.uploadImage(image: postImage, name: filename)
post.mediaFilename = filename
}
var postTags:[Tag] = []
if let tokens = tagsView.tokens() {
for token in tokens {
let tagFetchRequest: NSFetchRequest<Tag> = Tag.fetchRequest()
tagFetchRequest.predicate = NSPredicate(format: "name == %#", token.title)
do {
let res = try managedContext.fetch(tagFetchRequest)
var tag: Tag!
if res.count > 0 {
tag = res.first
} else {
tag = Tag(context: managedContext)
tag.name = token.title
tag.mostRecentUpdate = NSDate()
tag.mostRecentThumbnail = postImage?.jpegData(compressionQuality: 1.0) as NSData?
}
postTags.append(tag)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return
}
}
}
for tag in postTags {
post.addToTags(tag)
}
post.isVideo = isVideo!
post.thumbnail = pickedPostThumbnail?.jpegData(compressionQuality: 1.0) as NSData?
post.notes = notesView.text
post.timeStamp = postDate! as NSDate
do {
try managedContext.save()
dismiss(animated: true, completion: nil)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
I am doing a lot of stuff there, but notably, I am setting the post.thumbnail with a UIImage as NSData? and at the end I am saving the context.
So I create posts and close the app, then open it again and another view controller does this:
fileprivate lazy var posts: NSFetchedResultsController<Post> = {
let appDelegate =
UIApplication.shared.delegate as? AppDelegate
let managedContext =
appDelegate?.persistentContainer.viewContext
let request: NSFetchRequest<Post> = NSFetchRequest(entityName: "Post")
request.predicate = NSPredicate(format: "%# IN self.tags", tag)
let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
request.sortDescriptors = [timeSort]
let posts = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
posts.delegate = self
return posts
}()
override func viewDidLoad(){
....
do {
try posts.performFetch()
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
...
}
let post = posts.object(at: indexPath)
cell.mediaFileName = post.mediaFilename
if let data = post.thumbnail {
print(data,"data?")
cell.thumbnail.image = UIImage.init(data: data as Data)
} else {
print("couldn't find")
}
This works fine the first few restarts. However soon, it starts printing ("couldn't find") and not loading the image. The other fields associated with post are still loading. This is being run on the iPhone 8 simulator that comes with Xcode.
What am I doing wrong here? Is there something about how a simulator stores images in core data that doesn't persist? Or am I misusing the managed context?

sort coreData fetch (swift4)

Right now my code is using NSPredicate or sort descriptor to basically just collect a name. I would like NSPredicate to sort all of the names from a-z thats it.
COREDATAHANDLER
class coreDataHandler: NSObject {
private class func getContext() -> NSManagedObjectContext{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
class func saveObject(username:String, password:String) -> Bool{
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
let managedObject = NSManagedObject(entity: entity!, insertInto: context)
managedObject.setValue(username, forKey: "username")
managedObject.setValue(password, forKey: "password")
do {
try context.save()
return true
} catch {
return false}
}
class func filterData() -> [User]? {
let conttext = getContext()
let fetchRequest:NSFetchRequest<User> = User.fetchRequest()
var user:[User]? = nil
var predicate = NSPredicate(format: "username contains[d] %#" ,"duke")
fetchRequest.predicate = predicate
do {
user = try conttext.fetch(fetchRequest)
return user
}catch {
return user
}
}
VIEWCONTROLLER
user = coreDataHandler.filterData()
for i in user! {
print("Judou : \(i.username!)")
}
If you want your user array to be sorted by username and then password, I would add a class function to your CoreDataHandler class to get a sorted array:
class func getSortedData() -> [User]? {
let conttext = getContext()
let fetchRequest:NSFetchRequest<User> = User.fetchRequest()
var user:[User]? = nil
var nameSort = NSSortDescriptor(key:"username", ascending:true)
var passwordSort = NSSortDescriptor(key:"password", ascending:false)
fetchRequest.sortDescriptors = [nameSort, passwordSort]
do {
user = try conttext.fetch(fetchRequest)
return user
} catch {
return user
}
}
Then in your view controller, use this new function to get the sorted array:
user = coreDataHandler.getSortedData()
for i in user! {
print("Judou : \(i.username!), Password:\(i.password)")
}
In swift 4 or swift 5, you can use
func sortlist(){
//1
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
//2
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "User")
let sort = NSSortDescriptor(key: "username", ascending: false)
fetchRequest.sortDescriptors = [sort]
//3
do {
let langugeCodes = try managedContext.fetch(fetchRequest)
for result in langugeCodes as [NSManagedObject] {
var username:String = result.value(forKey: "username")! as! String
var password:String = result.value(forKey: "password")! as! String
print("username==>",username)
print("name==>",name)
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}

Update core data object swift 3

I want to update a core data object in swift 3. After some googled I didn't found anything about swift 3.
So my question is: how can I update a core data object in swift 3?
Fetch the existing values using a fetch request with a predicate. Use a unique value in the predicate. Once you've fetched the object, update the object with new values and save the context.
let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '\(empId)'")
fetchRequest.predicate = predicate
do {
let result = try persistentContainer.viewContext.fetch(fetchRequest)
if let objectToUpdate = result.first as? NSManagedObject {
objectToUpdate.setValue("newName", forKey: "name")
objectToUpdate.setValue("newDepartment", forKey: "department")
objectToUpdate.setValue("001", forKey: "empID")
try persistentContainer.viewContext.save()
}
} catch {
print(error)
}
Using NSManagedObject subclass
let empId = "001"
let fetchRequest: NSFetchRequest<Employee> = Employee.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "%K = %#", #keyPath(Employee.id), empId)
do {
let results = try persistentContainer.viewContext.fetch(fetchRequest)
if let employee = results.first {
employee.name = "new name"
employee.department = "new department"
}
try persistentContainer.viewContext.save()
} catch let error as NSError {
print(error.localizedDescription)
}
Batch updates
Batch updates help to update multiple Core Data objects without having
to fetch anything into memory.
let batchUpdate = NSBatchUpdateRequest(entityName: "Employee")
batchUpdate.propertiesToUpdate = [#keyPath(Employee.isActive): true]
batchUpdate.affectedStores = persistentContainer.viewContext.persistentStoreCoordinator?.persistentStores
batchUpdate.resultType = .updatedObjectsCountResultType
do {
let batchResult = try coreDataStack.managedContext.execute(batchUpdate) as? NSBatchUpdateResult
print(batchResult?.result)
} catch let error as NSError {
print(error.localizedDescription)
}
Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:
func context() -> NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}
func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)
let result = try? context().fetch(fetchRequest)
if result?.count == 1 {
let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}

Swift 3 Core Data Delete Object

Unfortunately the new Core Data semantics make me crazy. My previous question had a clean code that didn't work because of incorrect auto generation of header files. Now I continue my work with deleting objects.
My code seems to be very simple:
func deleteProfile(withID: Int) {
let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
let object = try! context.fetch(fetchRequest)
context.delete(object)
}
I did a "hard" debug with print(object) instead of context.delete(object) and it showed me the right object.
So I need just to delete it.
P.S. there is no deleteObject. Now NSManagedContext has only public func delete(_ sender: AnyObject?)
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 {
context.delete(object)
}
}
do {
try context.save()
} catch {
//Handle error
}
If no matching objects exist then the fetch succeeds, but the resulting
array is empty.
Note: In your code, object has the type [Event] and therefore in
context.delete(object)
the compiler creates a call to the
public func delete(_ sender: AnyObject?)
method of NSObject instead of the expected
public func delete(_ object: NSManagedObject)
method of NSManagedObjectContext. That is why your code compiles
but fails at runtime.
The trick here, it is save context after deleting your objects.
let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
let objects = try! context.fetch(fetchRequest)
for obj in objects {
context.delete(obj)
}
do {
try context.save() // <- remember to put this :)
} catch {
// Do something... fatalerror
}
I hope this can help someone.
func deleteRecords() {
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.persistentContainer.viewContext
let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "nameofentity")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
do {
try context.execute(deleteRequest)
try context.save()
} catch {
print ("There was an error")
}
}
Delete core data objects swift 3
// MARK: Delete Data Records
func deleteRecords() -> Void {
let moc = getContext()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
let result = try? moc.fetch(fetchRequest)
let resultData = result as! [Person]
for object in resultData {
moc.delete(object)
}
do {
try moc.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
// MARK: Get Context
func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
Swift 4.1, 4.2 and 5.0
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let requestDel = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
requestDel.returnsObjectsAsFaults = false
// If you want to delete data on basis of some condition then you can use NSPredicate
// let predicateDel = NSPredicate(format: "age > %d", argumentArray: [10])
// requestDel.predicate = predicateDel
do {
let arrUsrObj = try context.fetch(requestDel)
for usrObj in arrUsrObj as! [NSManagedObject] { // Fetching Object
context.delete(usrObj) // Deleting Object
}
} catch {
print("Failed")
}
// Saving the Delete operation
do {
try context.save()
} catch {
print("Failed saving")
}
Swift 4 without using string for Entity
let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
do {
let objects = try context.fetch(fetchRequest)
for object in objects {
context.delete(object)
}
try context.save()
} catch _ {
// error handling
}
Delete Core Data Object with query in Swift 5, 4.2
let fetchRequest = NSFetchRequest<Your_Model>(entityName: "Your_Entity_Name")
fetchRequest.predicate = NSPredicate(format: "any your_key == %d", your_value)
hope this will help to someone
Swift 5
Common function for Deleting core data objects for any anity swift 5
func deleteEntityObjectByKeyValue<T>(className: T.Type, key: String, value: Any) -> Bool {
let context = CoreDataStack.sharedStack.mainContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: className.self))
//Search based on data type.I used for Int and String
if let sValue = value as? String {
let predicate = NSPredicate(format: "\(key) == %#", sValue)
fetchRequest.predicate = predicate
} else if let iValue = value as? Int64 {
let predicate = NSPredicate(format: "\(key) == %d", iValue)
fetchRequest.predicate = predicate
}
do {
let result = try context.fetch(fetchRequest)
if result.count != 0 {
if let managedObject = result[0] as? NSManagedObject {
context.delete(managedObject)
do {
try context.save()
return true
}
catch let error {
print(error.localizedDescription)
}
}
}
return false
} catch let error {
print(error.localizedDescription)
}
return false
}
How To Use it:
let isSaved = CoreDataOperations.shared.deleteEntityObjectByKeyValue(className: EmpolyeeData.self, key: "employeeId", value:1234)
If isSaved {
print("Deleted obj Successfully")
}
Swift 4,5
It is useful to delete particular record or all records from entity.
1.Create a NSPersistentContainer and NSManagedObjectContext using below code.
class CoreDataStack: NSObject {
static var sharedStack = CoreDataStack()
private override init() {}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Employee")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let url = storeDescription.url {
print("SQLITE STORE LOCATION: \(url.absoluteString)")
}
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.shouldDeleteInaccessibleFaults = true
container.viewContext.automaticallyMergesChangesFromParent = true
return container
}()
private lazy var applicationDocumentsDirectory: URL = {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1]
}()
private lazy var managedObjectModel: NSManagedObjectModel = {
let modelURL = Bundle.main.url(forResource: "____", withExtension: "____")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
public lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.appendingPathComponent("Employee" + ".sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
}
return coordinator
}()
lazy var mainContext: NSManagedObjectContext = {
var managedObjectContext: NSManagedObjectContext?
if #available(iOS 10.0, *){
managedObjectContext = self.persistentContainer.viewContext
managedObjectContext?.mergePolicy = NSMergePolicy.init(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType)
}
else{
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext?.persistentStoreCoordinator = coordinator
managedObjectContext?.mergePolicy = NSMergePolicy.init(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType)
}
return managedObjectContext!
}()
}
2.Common function for Deleting core data all objects for any Entity swift 5
func deleteEntityData(entity : String) {
let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
do {
try CoreDataStack.sharedStack.mainContext.execute(deleteRequest)
CoreDataStack.sharedStack.saveMainContext()
} catch {
print ("There was an error")
}
}
3.How to use above code.
self.deleteEntityData(entity : "Employee")
4.If you want to delete Particular object from Entity
func deleteEntityObjectByKeyValue<T>(entityName: T.Type, key: String, value: Any) -> Bool {
let context = CoreDataStack.sharedStack.mainContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: entityName.self))
if let sValue = value as? String {
let predicate = NSPredicate(format: "\(key) == %#", sValue)
fetchRequest.predicate = predicate
} else if let iValue = value as? Int64 {
let predicate = NSPredicate(format: "\(key) == %d", iValue)
fetchRequest.predicate = predicate
}
do {
let result = try context.fetch(fetchRequest)
if result.count != 0 {
if let managedObject = result[0] as? NSManagedObject {
context.delete(managedObject)
do {
CoreDataStack.sharedStack.saveMainContext()
return true
}
catch let error {
print(error.localizedDescription)
}
}
}
return false
} catch let error {
print(error.localizedDescription)
}
return false
}
Delete the object from core data
let entity = NSEntityDescription.entity(forEntityName: "Students", in: managedContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entity
if let result = try? managedContext.fetch(request) {
for object in result {
managedContext.delete(object as! NSManagedObject)
}
txtName.text = ""
txtPhone.text = ""
txt_Address.text = ""
labelStatus.text = "Deleted"
}

Saving object to core data with NSPredicate

I'm trying to save an object to Core Data which is fetched from Parse, but i'd like only those which doesn't contain a name which is already in database:
func fetchFromParse() {
let entity = NSEntityDescription.entityForName("Medicine", inManagedObjectContext: context)
let query = PFQuery(className: "Medicine")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
for object in objects! {
let medicine = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.context)
if let name = object["medicineName"] as? String,
amount = object["amountQuantity"] as? String {
let predicate = NSPredicate(format: "name != %#", name)
self.fetchRequest.predicate = predicate
do{
let fetchedEntities = try self.context.executeFetchRequest(self.fetchRequest) as! [Medicine]
//save to Core Data
medicine.setValue(name, forKey: "name")
medicine.setValue(amount, forKey: "amount")
} catch let error as NSError{
print(error)
}
do {
try self.context.save()
self.tableView.reloadData()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}
}
}
}
Fetch data from Core Data
func fetchFromCoreData() {
do {
let results = try context.executeFetchRequest(fetchRequest)
medicines = results as! [Medicine]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
i put the in viewWillAppear:
if Reachability.isConnectedToNetwork() {
//fetching data from Parse
fetchFromParse()
fetchFromCoreData()
tableView.reloadData()
} else {
//fetching data from Core data
fetchFromCoreData()
logOutButton.enabled = false
}
But it doesn't work. Any suggestions how to repair it ?
You kind of have everything, just not hooked up. You shouldn't be instantiating and inserting the new object until after you've executed and checked the fetch. You currently execute the fetch but don't check to see if there is a duplicate. Both of these issues need to be resolved, something along the lines of:
let entity = NSEntityDescription.entityForName("Medicine", inManagedObjectContext: context)
let query = PFQuery(className: "Medicine")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
for object in objects! {
if let name = object["medicineName"] as? String,
amount = object["amountQuantity"] as? String {
let predicate = NSPredicate(format: "name = %#", name)
self.fetchRequest.predicate = predicate
do{
let fetchedEntities = try self.context.executeFetchRequest(self.fetchRequest) as! [Medicine]
//save to Core Data
if fetchedEntities.count <= 0 {
let medicine = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.context)
medicine.setValue(name, forKey: "name")
medicine.setValue(amount, forKey: "amount")
}
} catch let error as NSError{
print(error)
}
}
}
do {
try self.context.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}

Resources