Swift 从解析脱机时如何获取NSUserDefaults中的数据
我有一个项目,可以在Parse中查找作为数组的对象,并在应用程序启动完成时获取它。但它无法在没有网络连接的情况下获取信息,因此我考虑使用NSUserDefaults,首先将数据保存到NSUserDefaults,并在脱机时从NSUserDefaults获取数据。 谁能给我举个逻辑的例子吗 这是我写的代码,我不知道脱机时如何获取它。 我想从解析中附加数据的数组将是[[String]]Swift 从解析脱机时如何获取NSUserDefaults中的数据,swift,parse-platform,nsuserdefaults,Swift,Parse Platform,Nsuserdefaults,我有一个项目,可以在Parse中查找作为数组的对象,并在应用程序启动完成时获取它。但它无法在没有网络连接的情况下获取信息,因此我考虑使用NSUserDefaults,首先将数据保存到NSUserDefaults,并在脱机时从NSUserDefaults获取数据。 谁能给我举个逻辑的例子吗 这是我写的代码,我不知道脱机时如何获取它。 我想从解析中附加数据的数组将是[[String]] let userDefaults = NSUserDefaults.standardUserDefault
let userDefaults = NSUserDefaults.standardUserDefaults
let query = ClassSub.query()
query.whereKey("nameOfData", containsString: "testString")
query!.findObjectsInBackgroundWithBlock { (busObjects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
print(error)
}
for object in testObjects! {
let testData = object["arrayData"] as? [[String]]
for i in 0..<testData!.count {testData
testArrayappend(testData![i])
self.userDefaults.setObject(NSKeyedArchiver.archivedDataWithRootObject(testData[i]), forKey: "test-time")
}
}
}
let userDefaults=NSUserDefaults.standardUserDefaults
let query=ClassSub.query()
whereKey(“nameOfData”,containssString:“testString”)
查询findObjectsInBackgroundWithBlock{(busObjects:[PFObject]?,错误:NSError?->Void in
如果错误!=nil{
打印(错误)
}
对于testObjects中的对象{
将testData=object[“arrayData”]设为?[[String]]
对于0中的i..我认为您最好使用parse ios SDK本地数据存储。本地数据存储允许您将解析对象固定到本地数据库中,然后当您离开网络时,您仍然可以从本地数据库获取数据。另一个巨大的优势是SaveFinally功能,它允许您在访问时保存对象重新脱机,然后在联机后将其同步回服务器
要使用本地数据存储功能,您需要执行以下步骤:
在解析配置中启用本地数据存储
let configuration = ParseClientConfiguration {
$0.applicationId = "{PARSE_APP_ID}"
$0.server = "http://localhost:1337/parse"
$0.localDatastoreEnabled = true
}
Parse.initializeWithConfiguration(configuration)
如果要从本地数据存储进行查询(脱机时),只需在调用findObjectsInBackground之前调用其他函数,因此代码应如下所示:
let query = PFQuery(className: "MyParseObjectClassName")
// if you are in offline mode then make sure the query
// will access your local data store and not to the server
if (offlineMode){
query.fromLocalDatastore()
}
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
// do something with the result
}
let query = PFQuery(className: "MyParseObjectClassName")
// if you are in offline mode then make sure the query
// will access your local data store and not to the server
if (self.offlineMode){
query.fromLocalDatastore()
}
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if (!self.offlineMode){
// pin all objects
PFObject.pinAllInBackground(objects)
// pin only first object
let obj = objects?.first
obj?.pinInBackground()
}
}
使用本地数据存储时,需要锁定从服务器获取的对象。要锁定对象,只需调用PFObject下的pinInBackground()。也可以使用pinAllInBackground()要在一次调用中将多个对象固定到本地数据存储,请执行以下操作:
let query = PFQuery(className: "MyParseObjectClassName")
// if you are in offline mode then make sure the query
// will access your local data store and not to the server
if (offlineMode){
query.fromLocalDatastore()
}
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
// do something with the result
}
let query = PFQuery(className: "MyParseObjectClassName")
// if you are in offline mode then make sure the query
// will access your local data store and not to the server
if (self.offlineMode){
query.fromLocalDatastore()
}
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if (!self.offlineMode){
// pin all objects
PFObject.pinAllInBackground(objects)
// pin only first object
let obj = objects?.first
obj?.pinInBackground()
}
}
现在,为了知道你们是离线还是在线,我建议你们使用图书馆。
此库为您提供两个功能块:联机和脱机。使用这些功能块,您可以确定应用程序何时连接到internet以及何时未连接到internet。因此,当应用程序未连接到internet时,您需要将offlineMode标志设置为true,从现在起,所有查询都将针对您的应用程序本地数据库,否则它将对您的服务器起作用。请阅读文档()我不是说我不知道如何从NSUSedefaults获取数据,我是说离线时的逻辑谢谢你简洁的解释。但我宁愿不依赖解析,因为它明年就要结束了。你有什么理由不推荐使用NSUSedefaults吗?HI@yanamstyle accord对于你的代码,你正在使用parse iOS SDK。如果你真的不想依赖parse,你应该在parse REST API的基础上创建一个抽象层,除非你在幕后进行某种映射工作,否则不要使用parse SDK。此外,parse正在关闭,但他们将parse.com代码作为一个名为pa的开放源代码发布rse服务器。parse服务器可以部署到任何云提供商,并包含parse.com的所有功能以及更多功能。因此,如果您愿意,您可以将其用作解决方案或临时解决方案,直到您开发自己的后端。很抱歉,没有进行研究,但即使parse-server是开放的,此parse本地数据存储是否可用ce?我听说在服务结束后很少有选项不可用,即使它是一个开源的parse local data store是客户端功能,而不是服务器,所以当您使用parse server而不是parse.com时,它也是可用的