覆盖核心数据值

时间:2017-02-17 09:37:36

标签: ios iphone swift core-data

你好我需要覆盖private void init() { final FragmentManager fragmentManager = getSupportFragmentManager(); fragment = fragmentManager.findFragmentById(R.id.fragment_chat); final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.hide(fragment); fragmentTransaction.commit(); } public void showChatForm() { showHideFragment(fragment); } private void showHideFragment(final Fragment fragment) { final FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction(); fragTransaction.setCustomAnimations(R.anim.fragment_slide_from_bottom, R.anim.fragment_slide_to_bottom); if (fragment.isHidden()) { fragTransaction.show(fragment); } else { fragTransaction.hide(fragment); } fragTransaction.commit(); } 上的值,如果已经存在的话。例如,如果已经在CoreData我有Josh我需要删除它,然后保存新的Josh

CoreData

2 个答案:

答案 0 :(得分:1)

我们可以检查值是否存在。然后在数据库中更新和创建值

class func checkAndUpdateAtIndex(entry_id: String, comment_text: String, comment_name: String, comment_pic: String, comment_date: String, comment_id: String, comment_isDeleted: String, comment_userid: String) {
    let context: NSManagedObjectContext = DataAccess.shared.managedObjectContext
    let request = NSFetchRequest(entityName: "Comment")
    request.predicate = NSPredicate(format: "comment_id = %@", comment_id)

    if let fetchResults = (try? DataAccess.shared.managedObjectContext.executeFetchRequest(request)) as? [NSManagedObject] {
        if fetchResults.count != 0{
            //update value
            var managedObject = fetchResults as! [Comment]
            managedObject[0].setValue(entry_id, forKey: "entry_id")
            managedObject[0].setValue(comment_text, forKey: "comment_text")
            managedObject[0].setValue(comment_name, forKey: "comment_name")
            managedObject[0].setValue(comment_pic, forKey: "comment_pic")

        }else {  
            // create new entity if not found
            let newItems = NSEntityDescription.insertNewObjectForEntityForName("Comment", inManagedObjectContext: context) as! Comment

            newItems.entry_id = entry_id
            newItems.comment_text = comment_text
            newItems.comment_name = comment_name
            newItems.comment_pic = comment_pic            
        }


            do {
                try context.save()
            } catch {}
    }
}

在此更新方法中,我首先获取要更新的实体,然后在该位置存储新值。

答案 1 :(得分:1)

  • 获取对象。
  • 创建新对象(如果没有)。
  • 更新属性。
  • 保存上下文。
func save(value:String, for key:String) {
    let context = getContext()
    let request = NSFetchRequest(entityName: "Lastname")
    let predicate = NSPredicate(format:"%K == %@", value, key)
    request.predicate = predicate
    do {
       let fetchResults = try context.executeFetchRequest(request)
       let object : NSManagedObject 
       if fetchResults.isEmpty {
          let entity = NSEntityDescription.entity(forEntityName: "Lastname", in: context)
          object = NSManagedObject(entity: entity!, insertInto: context)
       } else }
          object = fetchResults[0]
       }

       object.setValue(value, forKey: key)

       try context.save()
       print("save ->  \(value)")
    } catch {
        print("err \(error)")
    }
}

注意:我不再使用Swift 2,可能会发生编译错误。