Ios 如何使用coredata脱机保存数据,如果网络(联机)出现,如何将保存的数据发送到API?
如何使用coredata在脱机状态下保存数据,如果网络出现(联机),如何将保存的数据发送到API 这是保存数据的代码:Ios 如何使用coredata脱机保存数据,如果网络(联机)出现,如何将保存的数据发送到API?,ios,swift,api,offline,reachability,Ios,Swift,Api,Offline,Reachability,如何使用coredata在脱机状态下保存数据,如果网络出现(联机),如何将保存的数据发送到API 这是保存数据的代码: func savePSaveInfo(cId: String, pId:String, createdBy:String, agep:String, languageId:String, emailId:String, mobileNumber:String,backendUpdate: Bool) { let nameObj = NSEntityDescrip
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")
}
}
//标记:-用于检索或获取数据
func retrieveData(){
//正如我们所知,容器是在AppDelegates中设置的,因此我们需要引用该容器。
让appDelegate=UIApplication.shared.delegate作为?appDelegate else{return}
//我们需要从这个容器中创建一个上下文
让managedContext=appDelegate.persistentContainer.viewContext
//为实体准备NSFetchRequest类型的请求
let fetchRequest=NSFetchRequest(entityName:“用户”)
做{
let result=尝试managedContext.fetch(fetchRequest)
arrUserDetails.removeAll()
对于结果中的数据![NSManagedObject]{
打印(data.value(forKey:“username”)为!字符串)
arrUserDetails.append(userData(用户名:(data.value(forKey:“用户名”)作为!字符串),email:(data.value(forKey:“email”)作为!字符串,password:(data.value(forKey:“密码”)作为!字符串)
}
tblView.reloadData()
}抓住{
打印(“失败”)
}
}
仍然存在任何问题,请让我了解查询请分享您为此所做的工作。基本上,您应该创建一个类似于
ConnectivityService
的服务来检查internet连接,如果它处于脱机状态,您应该通过CoreData
保存数据,然后您可以观察来自ConnectivityService
的通知。当网络上线时,您可以将数据从<代码> CordaDATA/code >发送到API,请考虑如下:StasOfFuff.com/Apple / Hooto to Advices并提供可重复的可复制代码,因为这不是“我们为您免费编写代码”-平台,而是“我们帮助您提供您的代码”-平台