如何使用 coredata 离线保存数据,如果网络来(在线)将保存的数据发送到 API?

How to save data offline using coredata, and if network comes(online) send the saved data to the API?

如何使用 coredata 离线保存数据,如果网络来(在线)将保存的数据发送到 API?

这是保存数据的代码:

func savePSaveInfo(cId: String, pId:String, createdBy:String, agep:String, languageId:String, emailId:String, mobileNumber:String,backendUpdate: Bool)
{    
    let nameObj = NSEntityDescription.insertNewObject(forEntityName: "SaveInfo", into: context!) as! SaveInfo

    nameObj.cId = cId
    nameObj.pId = pId
    nameObj.createdBy = createdBy
    nameObj.agep = agep
    nameObj.languageId = languageId
    nameObj.emailId = emailId
    nameObj.mobileNumber = mobileNumber
    nameObj.backendUpdate = backendUpdate    

    do{
        try context!.save()
    }catch{
        print("not saved")
    }
}

在核心数据中插入值,我分享一个例子,你可以根据你的数据设置。

第 1 步:- 像这样设置实体

第 2 步:-

//MARK:- FOR CREATING NEW DATA
    func createData(){

        //As we know that container is set up in the AppDelegates so we need to refer that container.
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

        //We need to create a context from this container
        let managedContext = appDelegate.persistentContainer.viewContext

        //Now let’s create an entity and new user records.
        let userEntity = NSEntityDescription.entity(forEntityName: "User", in: managedContext)!

        //final, we need to add some data to our newly created record for each keys using


        let user = NSManagedObject(entity: userEntity, insertInto: managedContext)
        user.setValue(txtFldUsername.text!, forKeyPath: "username")
        user.setValue(txtFldEmail.text!, forKey: "email")
        user.setValue(txtFldPassword.text!, forKey: "password")

        retrieveData()
        //Now we have set all the values. The next step is to save them inside the Core Data

        do {
            try managedContext.save()

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

第 3 站:- 用于获取数据

 //MARK:- FOR RETRIEVING OR FETCHING THE DATA
    func retrieveData() {

        //As we know that container is set up in the AppDelegates so we need to refer that container.
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

        //We need to create a context from this container
        let managedContext = appDelegate.persistentContainer.viewContext

        //Prepare the request of type NSFetchRequest  for the entity
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")

        do {
            let result = try managedContext.fetch(fetchRequest)
            arrUserDetails.removeAll()
            for data in result as! [NSManagedObject] {
                print(data.value(forKey: "username") as! String)
                arrUserDetails.append(userData(username: (data.value(forKey: "username") as! String), email: (data.value(forKey: "email") as! String), password: (data.value(forKey: "password") as! String)))
            }
            tblView.reloadData()

        } catch {

            print("Failed")
        }
    }

还有什么问题,让我知道查询