How to update existing object in core data ? [Swift] - ios

I have preloaded data from a .csv file into coredata. I am fetching the data in the following way
var places:[Places] = []
in viewDidLoad :
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "Places")
do{
places = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Places]
}
catch let error as NSError{
print("Failed to retrieve record: \(error.localizedDescription)")
}
}
In the data there is an attribute isFavorite of type String whose initial value is false. I am changing the value of isFavorite on button click. I want to save the changes made by the user. How can i make this change persistent ?
Here is my button action
#IBAction func addToFavourites(sender: AnyObject) {
cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell
if cell.isFavouriteLabel.text! == "false" {
cell.isFavouriteLabel.text = "true"
}else if cell.isFavouriteLabel.text == "true"{
cell.isFavouriteLabel.text = "false"
}
}
Basically i want to set the value of places.isFavourite = cell.isFavoriteLabel.text and save to core data
EDIT : if i try this inside my button action method
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let place : Places = Places()
place.isFavourite = cell.isFavouriteLabel.text
do{
try managedObjectContext.save()
} catch let error as NSError{
print(error)
}
}
i am getting an error : Failed to call designated initializer on NSManagedObject class
if i simply add this code in the button action method
places.isFavourite = cell.isFavouriteLabel.text
i get this error : [Places] does not have a member named isFavourite

Your current code is:
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let place : Places = Places()
place.isFavourite = cell.isFavouriteLabel.text
do{
try managedObjectContext.save()
} catch let error as NSError{
print(error)
}
}
That would create a new place (if it worked), but you need to update an existing one.
You have the places returned from managedObjectContext.executeFetchRequest.
So you need to get something like places[index_of_the_cell_in_question].isFavourite = cell.isFavouriteLabel.text
and then managedObjectContext.save().

Use the save function of the NSManagedObjectContext:
places.isFavourite = cell.isFavoriteLabel.text
var error: NSError?
if managedObjectContext.save(&error) != true {
// Error
}

This is simple as this:
Find the entry you want to modify in places then save the core data context.
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation 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.
println("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
I suggest you used a manager to insert, fetch and delete entry in your core data.
import Foundation
import CoreData
class CoreDataHelper: NSObject {
class var shareInstance:CoreDataHelper {
struct Static {
static let instance:CoreDataHelper = CoreDataHelper()
}
return Static.instance
}
//MARK: - Insert -
func insertEntityForName(entityName:String) -> AnyObject {
return NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: self.managedObjectContext!)
}
//MARK: - Fetch -
func fetchEntitiesForName(entityName:String) -> NSArray {
...
}
//MARK: - Delete -
func deleteObject(object:NSManagedObject) {
self.managedObjectContext!.deleteObject(object)
}
// MARK: - Core Data Saving support -
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation 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.
println("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
Hop this can help you

Related

How to delete all data from core data when persistentContainer is not in App delegate method in swift

I am new in Swift and I want to delete all data from core data. I have seen several examples but in all of them persistentContainer is in AppDelegate and in my case persistentContainer is not in AppDelegate. It is in a different class as shown below:
class CoreDataStack: NSObject {
static let sharedInstance = CoreDataStack()
private override init() {}
func applicationDocumentsDirectory() {
if let url = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).last {
print(url.absoluteString)
}
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Database")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
print(storeDescription)
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
In the AppDelegate method I am just calling it as
CoreDataStack.sharedInstance.applicationDocumentsDirectory()
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
CoreDataStack.sharedInstance.saveContext()
}
I tried this code but it does not work for me.
One of the solutions can be to use NSBatchDeleteRequest for all entities in the persistentContainer. You can add these methods to your CoreDataStack:
func deleteAllEntities() {
let entities = persistentContainer.managedObjectModel.entities
for entity in entities {
delete(entityName: entity.name!)
}
}
func delete(entityName: String) {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try persistentContainer.viewContext.execute(deleteRequest)
} catch let error as NSError {
debugPrint(error)
}
}
The whole code can be found here
Need to use NSBatchDeleteRequest for particular entity from persistentContainer
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")
}
}
I have detailed an answer in below link.
enter link description here
A more radical approach is to remove/recreate your persistent stores.
extension NSPersistentContainer {
func destroyPersistentStores() throws {
for store in persistentStoreCoordinator.persistentStores {
let type = NSPersistentStore.StoreType(rawValue: store.type)
try persistentStoreCoordinator.destroyPersistentStore(at: store.url!, type: type)
}
loadPersistentStores()
}
func loadPersistentStores() {
loadPersistentStores { _, error in
if let error { fatalError(error.localizedDescription) }
}
}
}
Usage: try container.destroyPersistentStores()
Source

how to validate login form into next page using Sqllite in iOS?

**this is to how to create a login validation form to move from login to next view controller **
**fetching the data from database**
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
**it stores the data**
let managedContext = appDelegate.persistentContainer.viewContext
//it fetches the data
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Details")
**validation code to check it but the condition fails**
do {
let result = try managedContext.fetch(fetchRequest)
for data in result as! [NSManagedObject] {
if ([emailid.text].count != 0 && [password.text].count != 0){
if (emailid.text == data.value(forKey: "emailId") as? String) && (password.text == data.value(forKey: "passWord") as? String){
let secondvc = storyboard?.instantiateViewController(withIdentifier: "loginVcID") as! loginVc
self.navigationController?.pushViewController(secondvc, animated: true)
}
in this condition it is not moving to next view controller
to check another condition
}
else {
self.label.text = "enter a valid data"
}
}
}
**when it fails it goes to catch to show that**
catch
{
print("Failed")
}
}
}
**this code is for registration to save into database**
**to create a database and store the value**
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let detailEntity = NSEntityDescription.entity(forEntityName: "Details", in: managedContext)!
** creation of database**
let detail = NSManagedObject(entity: detailEntity, insertInto: managedContext)
detail.setValue(username.text, forKeyPath: "userName")
detail.setValue(emailid.text, forKey: "emailId")
detail.setValue(password.text, forKey: "passWord")
detail.setValue(city.text, forKey: "city")
**saving the data**
do {
try managedContext.save()
}
** it display whatever in that method**
catch let error as NSError
{
its shows error when it fails
print("Could not save. (error), (error.userInfo)")
}
Go to you appDelegate, you will find a line // MARK: - Core Data stack & // MARK: - Core Data Saving support, remove the saveContext() function & also remove persistentContainer.. Then
Add this class to your project
final class PersistenceManager {
private init() {}
static let shared = PersistenceManager()
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "ProjectNAME")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() 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.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
lazy var context = persistentContainer.viewContext
// MARK: - Core Data Saving support
func save() {
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() 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.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func fetch<T: NSManagedObject>(_ objectType: T.Type) -> [T] {
let entityName = String(describing: objectType)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
do {
let fetchedObjects = try context.fetch(fetchRequest) as? [T]
return fetchedObjects ?? [T]()
} catch {
return [T]()
}
}
func deleteAll<T: NSManagedObject>(_ objectType: T.Type) {
let entityName = String(describing: objectType)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try persistentContainer.persistentStoreCoordinator.execute(deleteRequest, with: context)
} catch {
print(error.localizedDescription)
}
}
func delete(_ object: NSManagedObject) {
context.delete(object)
save()
}
}
To fetch data
PersistenceManager.shared.fetch(User.self)
To delete data
PersistenceManager.shared.delete(user)
To create user
let newUser = Users(context: PersistenceManager.shared.context)
newUser.name = "Zero Cool"
newUser.password = "qwerty"
PersistenceManager.shared.save()

How to Delete a managedObject - not a tableView

I am describing some color information in a Core Data diagram. The entity is a color, and the attributes are color components.
I am struggling with two aspects: how to delete an color object from the graph, and secondly, (bonus question?), how could I identify duplicate colors?
In my AppDelegate, I have a core data stack like this:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DD")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replacing this implementation with code to handle the error appropriately.
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
print(#function)
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() 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.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
and where I'm trying to delete the color, I have this:
func deleteColor(_ sender:UIButton) {
let i : Int = (sender.layer.value(forKey: "index")) as! Int
print(#function, "object to delete: ", i)
let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
colors.remove(at: i)
do {
try managedContext.save()
} catch let error as NSError {
print("Error While Saving Data: \(error.userInfo)")
}
recentColorCollection!.reloadData()
}
Variables are:
var colors = [RecentColorEntity]()
var colorEntity = "RecentColorEntity"
I'm not getting any errors, but the objects are not being deleted.. Can someone help me figure out what I'm doing wrong
colors.remove(at: i)
just removes the color from your colors array in memory. You need to delete the actual object, like this
context.delete(colorObject)
and save.

(iOS Swift) Fetching record from Core Data return fatal error or nil

I'm starting to code with Swift for iPhone apps, i'm facing this rather confusing obstacle that for some of you might be trivial. I keep getting error message that the record is either fatal error: unexpectedly found nil while unwrapping an Optional value or nil, but when i check the .sqlite the record is there
Let me walk you through
The name of my .xcdatamodeld is ReviewerApp.xcdatamodeld, the same as my app name ReviewApp
ReviewerApp.xcdatamodeld
My class name is Users and my Entity name is User
Class: Users, Entity: User
My Attributes (username, email, password) are all type: String, with properties: optional
Users.swift
import Foundation
import CoreData
#objc(Users)
class Users: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
#NSManaged var username: String?
#NSManaged var email: String?
#NSManaged var password: String?
}
The saveUser() function in signUpController.swift
func saveUser() {
// create an instance of our managedObjectContext
let moc = DataController().managedObjectContext
// we set up our entity by selecting the entity and context that we're targeting
let entity = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: moc) as! Users
// add our data
entity.setValue(usernameTxtFld.text, forKey: "username")
entity.setValue(emailTxtFld.text, forKey: "email")
entity.setValue(passwordTxtFld.text, forKey: "password")
// we save our entity
do {
try moc.save()
//user.append(entity)
print("saved")
} catch {
fatalError("Failure to save context: \(error)")
}
}
The fetch() function in SignInController.swift
let moc = DataController().managedObjectContext
let userFetch = NSFetchRequest(entityName: "User")
do {
let fetchedUser = try moc.executeFetchRequest(userFetch) as! [Users]
print(fetchedUser.first!.username!)
} catch {
fatalError("Failed to fetch person: \(error)")
}
Everytime i save the user registration process, the record is saved in core data. But everytime i am trying to fetch it:
by: print(fetchedUser.first!.username!) , the message in the console is fatal error: unexpectedly found nil while unwrapping an Optional value
by: print(fetchedUser.first?.username) , the message in the console is nil
Thank you very much for the help, in advance
First I would highly suggest you not to using '!' anywhere in your code as it will crash your app (only for static resources). You can use 'if let XXX = YYY as? [...]' to safely cast whatever you want...
Then you are probably not saving anything in your Database. How is your model? Yours Users class must be adequate to the model you built otherwise it won't work (which is a pain), also in your model you have to give your entity its class (in the 'class' field just below 'parent entity').
Also you don't have to use 'entity.setValue(usernameTxtFld.text, forKey: "username")' as you casted your entity beforehand. You could just 'entity.username =usernameTxtFld.text'.
If I didn't help, try to also add the xcdatamodel please.
I think you missed out on
let entityDescription = NSEntityDescription.entityForName("User", inManagedObjectContext: moc)
fetchRequest.entity = entityDescription
do {
let fetchedUser = try moc.executeFetchRequest(fetchRequest)
print(fetchedUser.first!.username!)
} catch {
let fetchError = error as NSError
print(fetchError)
}
The method returns an array of results if the fetch request is successful. Note that Core Data always returns an array if the fetch request is successful, even if we expect one result or if Core Data didn't find any matching records.
For more information and clarification visit - http://code.tutsplus.com/tutorials/core-data-and-swift-managed-objects-and-fetch-requests--cms-25068
The case is closed. There is some problem with the DataController.swift. It was called to save the data, but when it is called to fetch the data it somehow pointing to different place. So, what i did was
Not use the DataController.swift anymore
Equipped my AppDelegate.swift with // MARK: - Core Data Saving support for saving context, because i didn't set my app with including core data in the beginning
func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
// Replace this implementation 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.
let nserror = error as NSError
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}
I changed my saving function to
func saveUser() {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let entity = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: managedContext) as! Users
//3
entity.setValue(usernameTxtFld.text, forKey: "username")
entity.setValue(emailTxtFld.text, forKey: "email")
entity.setValue(passwordTxtFld.text, forKey: "password")
//4
do {
try managedContext.save()
//5
message = "Signing up is successful"
alertMessage(message)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
And changed my fetch function to
func fetch() {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "Users")
let predicate = NSPredicate(format: "email == %# AND password == %#", emailTxtFld.text!, passwordTxtFld.text!)
fetchRequest.predicate = predicate
//3
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
user = results as! [Users]
let row = user.count
if row > 0 {
message = "You're signed in"
//self.performSegueWithIdentifier("signInIdentifier", sender: self)
}else{
message = "Email & password combination is incorrect!"
}
alertMessage(message)
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}

CoreData Concurrency issue

I am having issue while using private managedObjectContextfor saving data in background. I am new to CoreData. I am using Parent-Child approach for NSManagedObjectContext but facing several issues.
Errors arise when I tap reload button multiple times
Errors:
'NSGenericException', reason: Collection <__NSCFSet: 0x16e47100> was mutated while being enumerated
Some times : crash here try managedObjectContext.save()
Sometimes Key value coding Compliant error
My ViewController class
class ViewController: UIViewController {
var jsonObj:NSDictionary?
var values = [AnyObject]()
#IBOutlet weak var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getData()
saveInBD()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.saved(_:)), name: "kContextSavedNotification", object: nil)
}
//Loding json data from a json file
func getData(){
if let path = NSBundle.mainBundle().pathForResource("countries", ofType: "json") {
do {
let data = try NSData(contentsOfURL: NSURL(fileURLWithPath: path), options: NSDataReadingOptions.DataReadingMappedIfSafe)
do {
jsonObj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
} catch {
jsonObj = nil;
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else {
print("Invalid filename/path.")
}
}
**Notification reciever**
func saved(not:NSNotification){
dispatch_async(dispatch_get_main_queue()) {
if let data = DatabaseManager.sharedInstance.getAllNews(){
self.values = data
print(data.count)
self.tableView.reloadData()
}
}
}
func saveInBD(){
if jsonObj != nil {
guard let nameArray = jsonObj?["data#"] as? NSArray else{return}
DatabaseManager.sharedInstance.addNewsInBackGround(nameArray)
}
}
//UIButton for re-saving data again
#IBAction func reloadAxn(sender: UIButton) {
saveInBD()
}
}
**Database Manager Class**
public class DatabaseManager{
static let sharedInstance = DatabaseManager()
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
private init() {
}
func addNewsInBackGround(arr:NSArray) {
let jsonArray = arr
let moc = managedObjectContext
let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = moc
privateMOC.performBlock {
for jsonObject in jsonArray {
let entity = NSEntityDescription.entityForName("Country",
inManagedObjectContext:privateMOC)
let managedObject = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: privateMOC) as! Country
managedObject.name = jsonObject.objectForKey("name")as? String
}
do {
try privateMOC.save()
self.saveMainContext()
NSNotificationCenter.defaultCenter().postNotificationName("kContextSavedNotification", object: nil)
} catch {
fatalError("Failure to save context: \(error)")
}
}
}
func getAllNews()->([AnyObject]?){
let fetchRequest = NSFetchRequest(entityName: "Country")
fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
do {
let results =
try managedObjectContext.executeFetchRequest(fetchRequest)
results as? [NSDictionary]
if results.count > 0
{
return results
}else
{
return nil
}
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
return nil
}
}
func saveMainContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
let nserror = error as NSError
print("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
You can write in background and read in the main thread (using different MOCs like you do). And actually you're almost doing it right.
The app crashes on the try managedObjectContext.save() line, because saveMainContext is called from within the private MOC's performBlock. The easiest way to fix it is to wrap the save operation into another performBlock:
func saveMainContext () {
managedObjectContext.performBlock {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
let nserror = error as NSError
print("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
Other two errors are a little more tricky. Please, provide more info. What object is not key-value compliant for what key? It's most likely a JSON parsing issue.
The first error ("mutated while being enumerated") is actually a nasty one. The description is pretty straight forward: a collection was mutated by one thread while it was enumerated on the other. Where does it occur?
One possible reason (most likely one, I would say) is that it is indeed a Core Data multithreading issue. Despite the fact that you can use several threads, you can only use core data objects within the thread they were obtained on. If you pass them to another thread, you'll likely run into an error like this.
Look through your code and try to find a place where such situation might occur (for instance, do you access self.values from other classes?). Unfortunately, I wasn't able to find such place in several minutes. If you said upon which collection enumeration this error occurs, it would help).
UPDATE:
P.S. I just thought that the error might be related to the saveMainContext function. It is performed right before a call to saved. saveMainContext is performed on the background thread (in the original code, I mean), and saved is performed on the main thread. So after fixing saveMainContext, the error might go away (I'm not 100% sure, though).
You are violating thread confinement.
You cannot write to CoreData in Background, and read in MainThread.
All operation on CoreData must be done in the same thread

Resources