加载,保存和重新加载数据的最佳方法 [英] The best way to load, save and reload data
问题描述
我认为我的iPhone应用程序存在一些性能问题.
I think I have got a little performance problem with my iPhone app.
我想向您展示如何从Internet加载数据,将其存储在手机上,然后重新加载以在表格视图中打印出来. 我在应用程序上有几个模块,所以我希望可以,如果我解释了我通常使用的方法,也许你们中的一个有个想法.
I like to show you how I load Data from the Internet, store them on the Phone and reload them to print it out on a table view. I have several modules on the app so I hope it is ok, if I explain the general way I use to do and maybe one of you got an idea.
数据量约为250个数据集,其中包含10-20个数据字段(整数/文本).
The amount of data is about 250 datasets with 10-20 data fields (integers / text).
步骤1:通过URL请求获取JSON数据字符串
我将通过具有一些限制值(例如city_id和last_update_time)的URL请求开始对MySQL数据库的请求,以限制结果.
I’ll start a request to an MySQL-Database by an URL-Request with some restrict values (for example city_id and last_update_time) to limit the result.
步骤2:将JSON对象保存到iPhone上的SQLite3数据库中.
之所以这样做,是因为我也想在离线模式下使用数据. 这部分应该是最重的部分,因为它需要总时间的3/4. 我遍历对象,并检查是否之前保存过该对象. (这里我使用import_id来识别MySQL-Database-Objects) 如果确实找到它,则将更新此数据集,否则将其插入.
I’ll do this because I like to use the data also in offline mode. This part should be the heavies’ one, because it takes 3/4 of the total time. I run through the objects and check if I saved this object once before. (Here I use an import_id to identify the MySQL-Database-Objects) If I did found it, I will update this dataset otherwise, I'll insert it.
第3步:从SQLite重新加载数据并将其放入NSMutableArray
此数组将由TableView使用.
This Array will be used by the TableView.
注意:数据将被加载到主线程中.其他线程将用于延迟加载一些拇指或显示旋转的车轮.
Note: The Data will be load in the main thread. Other threads will be used to lazy load some thumbs or showing a spinning wheel.
该程序是否存在重大错误?我应该在另一个线程中加载数据,但是此时我应该在视图中显示什么?
Is there a big mistake in the procedure? Should I load the data in another thread, but what should I display at this time in the view?
最好的方法是什么?
推荐答案
- 将一个json文件嵌入到您的应用程序包中,该文件包涉及json解析中的当前数据.
- Embed a json file into your app bundle that involves the current data from your json parse. This will be used for the first time offline using just for the case,
- Update your json file whenever the device is online, but only update if your parse version is different than you have.
- Use that json file anytime your device is offline
- Since your json file in your bundle will be the newest version all the time, load your table data out of it.
希望这些对您有帮助.
这篇关于加载,保存和重新加载数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!