快速存储临时数据的最佳实践 [英] Best practice for storing temporary data in swift
问题描述
我开发了一个应用程序,可以从服务器 获取大量JSON数据并在不同的VC上显示.我所做的是获取数据并将设置数据为静态变量,这样我就可以访问我所有应用程序中的数据.
I have developed an app that gets a lot of JSON data from server and shows on different VCs.What I did was getting data and setting data to static variables, so I can access data in all my app.
现在我注意到我可以使用 CoreData
或 UserDefaults
代替 Static variables
.
Now I noticed that I could have used CoreData
or maybe UserDefaults
instead of Static variables
.
我的问题是这种类型的应用的最佳做法是什么?为什么准确?"
My question is "What is the best practice for this type of apps and why exactly?"
注意:该应用无需脱机工作,也无需保存数据即可在脱机模式下显示.
NOTE: The app doesn't need to work offline and save data for showing in offline mode.
推荐答案
您不应将数据存储到UserDefaults,用户默认值只是基于键值的文件,该文件主要用于存储有关用户的某些数据,例如:>
You should not store data to UserDefaults, user defaults is just a key value based file which is mostly used to store some data about user, for example :
doNotShowAdds : true
isUserLogedIn :true.
您也不应该使用钥匙串,因为钥匙串是加密的容器,您可以在其中存储有关用户的评论者数据,例如:密码.
You should not use keychain either since keychain is encrypted container where u store critic data about user, for example : password.
您不需要使用数据库
我的意见是您不应该使用全局变量,主要是我尝试使viewController数据特定于其自身,不应从项目中的任何位置访问该数据. 何时在Swift中使用全局变量
My opinion is that you should not use global variables, mainly I try to make viewController data specific for itself, the data should not be accessed from anywhere in your project. When to use global variables in Swift
大多数情况下,我会通过特定视图控制器的实现来制定服务协议. 无论如何,每个控制器都应该有自己的一部分数据,例如
Mostly I make service protocols with implementations for specific view controllers. Anyways, each controller should have its own part of data for example
class MyViewControllerOne {
private var data :Array<DataExampleOne>;
}
class MyViewControllerTwo {
private var data :Array<DataExampleTwo>;
}
从API加载数据后,数据将一直存在,直到您关闭应用程序.
Once you load data from your API, the data will be there, until you close your app.
您总是可以制作另一个包含该数据的类,例如可能更干净的谓词:
You can always make another class which will contain this data and maybe predicates for cleaner approach for example :
protocol MyViewControllerOneService {
func search() -> Array<DataExampleOne>;
}
class MyViewControllerOneServiceImpl :MyViewControllerService {
public var data :Array<DataExampleOne>;
public func search(searchString :String) -> Array<DataExampleOne>{
let predicate = NSPredicate() ...
let filteredArray = self.data.filter { evaluate...}
return filteredArray;
}
}
我使用类似的方法,而不是将数据保存在我的服务类(业务逻辑)中,而是使用了用于从数据库获取数据的存储库.由于您没有任何数据库,因此这种方法是可以的.
I use similar approach, instead of saving data in my service classes (business logic), I got repositories which I use to get data from database. Since you don't have any database this approach is okay.
,然后代替
class MyViewControllerOne {
private var data :Array<DataExampleOne>;
}
您可以使用
class MyViewController {
private var myViewControllerService :MyViewControllerOneServiceImpl;
}
希望有帮助, 最好的问候!
Hope it helps, Best regards!
这篇关于快速存储临时数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!