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

问题描述 投票:-1回答:1

如何使用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")
    }
}
ios swift api offline reachability
1个回答
0
投票

为了在核心数据中插入值,我正在分享一个示例,您可以根据自己的数据进行设置。

步骤1:-设置像这样的实体enter image description here

步骤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")
        }
    }

仍然有任何问题,让我知道该查询

© www.soinside.com 2019 - 2024. All rights reserved.