web-dev-qa-db-ja.com

Swiftに既に保存されているCoreDataエントリをどのように更新しますか?

ここで何が間違っているのかはわかりませんが、初めてcoredataに保存すると、うまく機能します。上書きしようとしても、上書きされません。

func testStuff() {
    var token = loadLoginData()
    println("Token \(token)")
    saveLoginData("New Token")
    var newToken = loadLoginData()
    println("Token \(newToken)")
}

func saveLoginData(accessToken: String) {
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context: NSManagedObjectContext = appDel.managedObjectContext!
    // save data to core data
    var loginData = NSEntityDescription.insertNewObjectForEntityForName("LoginData", inManagedObjectContext: context) as NSManagedObject
    loginData.setValue(accessToken, forKey: "accessToken")
    context.save(nil)
    println("Done saving user")
}

/* Output
Token Optional("12345")
Done saving user
Token Optional("12345")
*/

SaveLoginデータを呼び出す関数にログインデータ関数をロードします

func loadLoginData() -> String? {
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context: NSManagedObjectContext = appDel.managedObjectContext!

    var request = NSFetchRequest(entityName: "LoginData")
    request.returnsObjectsAsFaults = false

    var results: NSArray = context.executeFetchRequest(request, error: nil)!
    if (results.count > 0) {
        var userData: NSManagedObject = results[0] as NSManagedObject
        var accessToken: String = userData.valueForKey("accessToken") as String

        return accessToken.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

    } else {
        println("0 results returned, potential error")
        return nil
    }
}
35
slooker

Batchupdateは、より大きなデータチャンクでより便利なので、これはより微妙なアプローチだと思います。

func saveLoginData(accessToken: String, userName: String) {
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context: NSManagedObjectContext = appDel.managedObjectContext!

    var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "LoginData")
    fetchRequest.predicate = NSPredicate(format: "userName = %@", userName)

    if let fetchResults = appDel.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSManagedObject] {
        if fetchResults.count != 0{

            var managedObject = fetchResults[0]
            managedObject.setValue(accessToken, forKey: "accessToken")

            context.save(nil)
        }
    }
}

私が間違っていない場合は、あなたの状況に少し翻訳しようとしましたが、テストしていません。

fetchRequest.predicateは基本的に、エンティティuserNameの属性LoginDataにフィルターを設定し、関数を呼び出すときに入力した(ユーザー)名を使用します。この例では、同じ名前のusernameが1つだけあると想定しています。次に、指定されたフィルターでfetchrequestを実行するため、関数を呼び出すときに入力するsetValueaccesTokenを使用して値を変更できます。後のコード:if fetchResults.count != 0usernameが存在する場合にのみ実行されます。

53
Henk-Martijn

Swift> = 2メソッドはオプションではないものを返し、エラーの場合はエラーをスローします。これはtry-catchで処理する必要があります。

    let context = self.fetchedResultsController.managedObjectContext
    let entity = self.fetchedResultsController.fetchRequest.entity!

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:entity.name!)
    fetchRequest.predicate = NSPredicate(format: "notificationId = 13")

    do {
        let list = try context.executeFetchRequest(fetchRequest) as? [NSManagedObject]
        if list!.count == 0 // Check notificationId available then not save
        {
            let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context)
            newManagedObject.setValue("This is first message13", forKey: "message")
            newManagedObject.setValue(1, forKey: "appId")
            newManagedObject.setValue(13, forKey: "notificationId")
            newManagedObject.setValue("First one", forKey: "tital")
        }
        // success ...
    } catch let error as NSError {
        // failure
        print("Fetch failed: \(error.localizedDescription)")
    }
8
Mitul Marsoniya

Swift 4&XCode 9.2に更新

質問に答えるには...

Swiftに既に保存されているCoreDataエントリをどのように更新しますか?

まず、AppDelegateviewContextへの参照を取得する必要があります。次に、更新するエンティティのNSFetchRequestを設定する必要があります。この例では、「アラート」になります。次に、フェッチを設定して、探している結果を見つけます。この例では、作成日とアラートタイプ別にアラートが見つかりました。

述部を使用して照会する方法をさらに読むため。 スタックオーバーフローの例Appleドキュメント

次にcontext.fetch(fetchRequest)を実行し、結果を更新したい値に設定し、try catchでエラーを処理しました。最後にcontext.save()

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Alert")

fetchRequest.predicate = NSPredicate(format: "creationDate = %@ AND alertType = %&",
                                         argumentArray: [creationDate, alertType])

do {
    let results = try context.fetch(fetchRequest) as? [NSManagedObject]
    if results?.count != 0 { // Atleast one was returned

        // In my case, I only updated the first item in results
        results[0].setValue(yourValueToBeSet, forKey: "yourCoreDataAttribute")
    }
} catch {
    print("Fetch Failed: \(error)")
}

do { 
    try context.save() 
   }
catch {
    print("Saving Core Data Failed: \(error)")
}
8
Devbot10

複数の新しいLoginDataオブジェクトを作成していますが、loadLoginDataメソッドは常に同じオブジェクトを返します。フェッチリクエストの結果の最初のオブジェクトです。

同じオブジェクトを更新し続けるため、saveLoginDetailsメソッドを変更する必要があります。

新しいオブジェクト(insertNewObjectForEntityName)を作成する代わりに、loadLoginDetailsメソッドを使用して既存のオブジェクトを取得し、そこでプロパティを変更します。

3
jrturton
 var context:NSManagedObjectContext = appDel.managedObjectContext!

 var en = NSEntityDescription.entityForName("ENTITIES_NAME", inManagedObjectContext: context)

 let batchUpdateRequest = NSBatchUpdateRequest(entity: en!)
            batchUpdateRequest.resultType = NSBatchUpdateRequestResultType.UpdatedObjectIDsResultType
            batchUpdateRequest.propertiesToUpdate = ["OBJECT_KEY": "NEWVALUE"]
            var batchUpdateRequestError: NSError?
            context.executeRequest(batchUpdateRequest, error:&batchUpdateRequestError)
            if let error = batchUpdateRequestError {println("error")}

幸運を

2
Mauricio Valdes

私にとってはうまくいきました、あなたはこれを試してみるべきです:

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Students", in: managedContext)
        let request = NSFetchRequest<NSFetchRequestResult>()
        request.entity = entity
        let predicate = NSPredicate(format: "(name = %@)", txtName.text!)
        request.predicate = predicate
        do {
            var results =
                try managedContext.fetch(request)
            let objectUpdate = results[0] as! NSManagedObject
            objectUpdate.setValue(txtName.text!, forKey: "name")
            objectUpdate.setValue(txtPhone.text!, forKey: "phone")
            objectUpdate.setValue(txt_Address.text!, forKey: "address")
            do {
                try managedContext.save()
                txtName.text = ""
                txtPhone.text = ""
                txt_Address.text = ""
                labelStatus.text = "Updated"
            }catch let error as NSError {
              labelStatus.text = error.localizedFailureReason
            }
        }
        catch let error as NSError {
            labelStatus.text = error.localizedFailureReason
        }
1
CSE 1994

それは私のために働いた、あなたはこれを試してみてください:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return}

let managedContext = appDelegate.persistentContainer.viewContext

// if object nil is checked if new entity will be created or created one will be updated

if object == nil {

// create new entity:


let entityObj = NSEntityDescription.entity(forEntityName: "EntityName", in: managedContext)!

let entity = NSManagedObject(entity: entityObj, insertInto: managedContext)


entity("new value", forKeyPath: "entityValue")

do {
    try managedContext.save()
    entities.append(entity)
    self.navigationController?.popViewController(animated: true)

} catch let error as NSError {
    print("Could not save. \(error), \(error.userInfo)")
}
}

else {

// the created entity will be updated selected object is entity -> came from previous view controller:

self.entity?.setValue("updated value", forKey: "entityValue")

do {
    try managedContext.save()

} catch let error as NSError {
    print("Could not save. \(error), \(error.userInfo)")
}

self.navigationController?.popViewController(animated: true)
}
0

これは私のために働いた。最初に、fetchRequest.predicateで指定された属性「taskName」でフィルターを設定します。次に、既に保存されているデータを取得し、「ステータス」の値を設定してそのオブジェクトを更新します。

func updateTaskStatus(status: Bool){

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()
    let predicate = NSPredicate(format: "(taskName = %@)", (task?.taskName)!)
    fetchRequest.predicate = predicate
    do {
        let result = try managedContext.fetch(fetchRequest)
        task = result[0] as NSManagedObject as? Task
        task?.setValue(status, forKey: "status")
        do {
            try managedContext.save()
        }catch  let error as NSError {
            print("\(error)")
        }
    }catch let error as NSError {
        print("\(error)")
    }
}
0
Sumona Salma

バッチ更新と呼ばれる新しい機能があります。

この記事はあなたに役立つと思います:

http://www.bignerdranch.com/blog/new-in-core-data-and-ios-8-batch-updating/

基本的には、NSBatchUpdateRequestの代わりにNSFetchRequestを使用し、NSPredicateで結果をフィルタリングし、結果の値を変更し、データを保存します。

Swiftの別のチュートリアル:

http://code.tutsplus.com/tutorials/ios-8-core-data-and-batch-updates--cms-22164

0
Dejan Skledar

ステップ:1-新しいプロジェクトを作成し、「コアデータを使用」を選択します:「 https://medium.com/@ankurvekariya/core-data-crud-with-Swift-4-2-for-beginners-40efe4e7d1cc "

ステップ:2-ViewController(userList)内

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDelegate , UITableViewDataSource {

var arrUser = Array<Any>()
@IBOutlet var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "User List"
    self.tableView.separatorStyle = .none
    self.retrieveData(Delete: false, indexpath: 0)

    let logoutBarButtonItem = UIBarButtonItem(image: UIImage(named: "addImage"), style: .done, target: self, action: #selector(addNewUser))
    self.navigationItem.rightBarButtonItem  = logoutBarButtonItem
}

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidDisappear(animated)
    self.retrieveData(Delete: false, indexpath: 0)
}

@objc func addNewUser(){
    let userVC = self.storyboard?.instantiateViewController(withIdentifier: "AddUser_VC") as! AddUser_VC
    self.navigationController?.pushViewController(userVC, animated: true)
}

//MARK: - TableView DataSource Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.arrUser.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell") as! UserCell
    cell.selectionStyle = .none
    let dictData = self.arrUser[indexPath.row] as? NSManagedObject
    cell.lblName.text = dictData?.value(forKey: "name") as? String ?? ""
    cell.lblPost.text = dictData?.value(forKey: "post") as? String ?? ""
    cell.lblEmail.text = dictData?.value(forKey: "email") as? String ?? ""
    cell.lblPhone.text = String(dictData?.value(forKey: "phone") as? Int ?? 0)
    cell.imageData?.image =  UIImage(data: dictData?.value(forKey: "image") as? Data ?? Data())

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 140
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let dictData = self.arrUser[indexPath.row] as? NSManagedObject
    let AddUserVC = self.storyboard?.instantiateViewController(withIdentifier: "AddUser_VC") as! AddUser_VC
    AddUserVC.isEdit  = true
    AddUserVC.dictObj = dictData
    self.navigationController?.pushViewController(AddUserVC, animated: true)

}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        //self.deleteData(indexpath: indexPath.row)
        self.retrieveData(Delete: true, indexpath: indexPath.row)
    }
}

//MARK: - retrieveData
func retrieveData(Delete:Bool , indexpath:Int) {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserTable")

    if (Delete == true)
    {
        do
        {
            let test = try managedContext.fetch(fetchRequest)

            let objectToDelete = test[indexpath] as! NSManagedObject
            managedContext.delete(objectToDelete)

            do{
                try managedContext.save()
                self.retrieveData(Delete: false, indexpath: 0)
            }
            catch
            {
                print(error)
            }
        }
        catch
        {
            print(error)
        }
    }

    do {
        self.arrUser = try managedContext.fetch(fetchRequest)
        self.tableView.reloadData()
        print(self.arrUser)
    } catch {
         print("Failed")
    }
}
}

ステップ:3-UserCell内

import UIKit

class UserCell: UITableViewCell {


@IBOutlet var lblName: UILabel!
@IBOutlet var lblEmail: UILabel!
@IBOutlet var lblPost: UILabel!
@IBOutlet var lblPhone: UILabel!
@IBOutlet var imageData: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    self.imageData.layer.cornerRadius = self.imageData.frame.height / 2
    self.imageData.layer.borderWidth = 1.0
    self.imageData.layer.borderColor = UIColor.lightGray.cgColor
    self.imageData.layer.masksToBounds = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

ステップ:4-AddUserVC内

import UIKit
import CoreData

class AddUser_VC: UIViewController ,UIImagePickerControllerDelegate ,UINavigationControllerDelegate {

var dictObj: NSManagedObject!
var isEdit:Bool = false

@IBOutlet var imageData: UIImageView!
@IBOutlet var txtName: UITextField!
@IBOutlet var txtEmail: UITextField!
@IBOutlet var txtPost: UITextField!
@IBOutlet var txtPhone: UITextField!

@IBOutlet var btnAddUser: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    self.imageData.layer.cornerRadius = self.imageData.frame.height / 2
    self.imageData.layer.borderWidth = 1.0
    self.imageData.layer.borderColor = UIColor.lightGray.cgColor
    self.imageData.layer.masksToBounds = true

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    self.imageData.isUserInteractionEnabled = true
    self.imageData.addGestureRecognizer(tapGestureRecognizer)

    if (self.isEdit == true)
    {
        self.txtName.text = dictObj.value(forKey: "name") as? String ?? ""
        self.txtEmail.text = dictObj.value(forKey: "email") as? String ?? ""
        self.txtPost.text = dictObj.value(forKey: "post") as? String ?? ""
        self.txtPhone.text = String(dictObj.value(forKey: "phone") as? Int ?? 0)
        self.imageData?.image = UIImage(data: dictObj?.value(forKey: "image") as? Data ?? Data())
        self.btnAddUser.setTitle("UPDATE", for: .normal)
    }
}


//MARK: - btnAddUserAction Method -
@IBAction func btnAddUserAction(_ sender: Any) {

    let arrData = [self.txtName,self.txtEmail,self.txtPost,self.txtPhone]
    for txt in arrData
    {
        if (txt?.text == "")
        {
            let alert = UIAlertController(title: "Alert", message: "Please Enter All Fields", preferredStyle: UIAlertController.Style.alert)
            alert.addAction(UIAlertAction(title: "Click", style: UIAlertAction.Style.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return
        }
    }

    self.createData()
}

//MARK: - Image Tap Method -
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallery()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true, completion: nil)
}

func openCamera()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallery()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.allowsEditing = true
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}


func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard (info[.originalImage] as? UIImage) != nil else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }
     self.imageData.image = info[.originalImage] as? UIImage
    picker.dismiss(animated: true, completion: nil)
}

//MARK: - createData
func createData(){

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let userEntity = NSEntityDescription.entity(forEntityName: "UserTable", in: managedContext)!

    var user = NSManagedObject()
    if(isEdit == true)
    {
         user = self.dictObj
    }
    else
    {
        user = NSManagedObject(entity: userEntity, insertInto: managedContext)
    }

    let image = self.imageData.image!.jpegData(compressionQuality: 0.5)as NSData?
    user.setValue(self.txtName.text, forKeyPath: "name")
    user.setValue(self.txtEmail.text, forKey: "email")
    user.setValue(self.txtPost.text, forKey: "post")
    user.setValue(Int(self.txtPhone.text!), forKey: "phone")
    user.setValue(image, forKey: "image")

    do {
        try managedContext.save()
        self.navigationController?.popViewController(animated: true)

    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
}

} 
0
Tej Patel