Reputation: 97
I'm developing an app to store one phone number at time using core data, the user should be able to enter a new phone number into ui text field,if it's equal to nil, it should store a new phone number,else it should replace old number with new number; it should store only one value.
but the code doesn't work as it should
what's wrong in my code?
let moContext: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = moContext.managedObjectContext
let phoneNu = NSEntityDescription.insertNewObjectForEntityForName("Setting", inManagedObjectContext: context)
let fetchRequest = NSFetchRequest(entityName: "Setting")
fetchRequest.predicate = NSPredicate(format: "phoneNumber = %@", phoneNumber)
// phoneNu.setValue(phoneNumber.text, forKey: "phoneNumber")
do{
let request = NSFetchRequest(entityName: "Setting")
let phoneN = try context.executeFetchRequest(request)
if phoneN.count == 0{
phoneNu.setValue(phoneNumber.text, forKey: "phoneNumber")
}else if phoneN.count != 0{
for item in phoneN as! [NSManagedObject]{
let number = item.valueForKey("phoneNumber")
number?[0]?.setValue(phoneNumber.text, forKey: "phoneNumber")
}
}
}catch{
print("error")
}
do{
let request = NSFetchRequest(entityName: "Setting")
let phoneNumber = try context.executeFetchRequest(request)
if phoneNumber.count > 0{
for item in phoneNumber as! [NSManagedObject]{
let number = item.valueForKey("phoneNumber")
print(number!)
}
}
}catch{
}
Upvotes: 0
Views: 636
Reputation: 861
This is my solution
let moContext: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = moContext.managedObjectContext
guard let phoneNumber = phoneNumber.text where phoneNumber.characters.count > 0 else {
return// returns if textField is empty.
}
let fetchRequest = NSFetchRequest(entityName: "Setting")
fetchRequest.predicate = NSPredicate(format: "phoneNumber = %@", phoneNumber)
do{
let request = NSFetchRequest(entityName: "Setting")
let phoneObjectArray = try context.executeFetchRequest(request)
guard let settingFetched = phoneObjectArray as? [Setting] where settingFetched.count > 0 else {
// if enters here -> 0 phone numbers were found
if let setting = NSEntityDescription.insertNewObjectForEntityForName("Setting", inManagedObjectContext: context) as? Setting {
setting.phoneNumber = phoneNumber
// call context.save()
}
return // this I forgot
}
let settingObject = settingFetched.first
// fetch results return store phone number.
// update phone number
settingObject.phoneNumber = phoneNumber
// call context.save()
} catch let error as NSError! {
print("error: \(error)")
}
You need to call the "save()" function to persist the information.
I did some changes to the name variables, added some validations and assume some model class name based on your code. I always recommend to use your NSManagedObject subclass (Setting I assume) instead of just using NSManagedObject and key/value coding.
There could be some minor syntax mistakes on the code because I was using a text editor.
Hope this helps!
Upvotes: 1
Reputation: 80273
For what you want to do, Core Data is overkill.
Use NSUserDefaults
instead. So much easier.
Upvotes: 1