将 Core Data 适配到我的应用程序有多容易?
我是一名 iPhone 开发新手,刚刚完成了我的第一个 iPhone 应用程序。 在配置我的 iPhone 进行开发后,我注意到该应用程序使用了太多内存,并且访问该应用程序中的 sqlite 数据库时发生的几次内存泄漏导致该应用程序经常崩溃。运行仪器后,我认为 CORE DATA 听起来是一个更好的方法:当然我仍然需要使用我拥有的 sqlite 数据库。
所以我的问题是:调整现有的应用程序以在核心数据上运行有多容易?我的应用程序基本上按县显示了 17,000 个不同的 mapView 注释,尽管我的数据库非常简单,基本上只是一个巨大的电子表格。
在应用程序委托中,我的应用程序打开 Sqlite 数据库,将一些数据放入具有四个属性的位置对象中,然后创建这些对象的数组。 第一个视图控制器让用户决定用户希望查看哪个县(十二个左右之一),然后最后一个视图控制器使用循环将选定的位置添加到地图视图中。
我应该如何修改上一段中的应用程序以使其使用 CoreData?您能否向我指出可以用来实现此目的的任何资源(最好不是开发中心的 CoreData 教程;过于混乱且比我现在需要的更复杂)?或者我必须在 xcode 中创建一个全新的项目并从头开始?
I'm a novice iphone developer, and just completed my first iphone app.
After provisioning my iphone for development, I noticed that the app used way too much memory, and that several memory leaks that were issuing from the app accessing the sqlite database in the app caused the app to crash often. After running instruments, I have decided that CORE DATA sounds like a better way to go: of course I still need to use the sqlite database that I have.
so my question is this: how easy would it be to adapt an already existing app to run on core data? My app basically shows 17,000 different mapView annotations in small amounts by county, though my database is very simple, just one giant spreadsheet basically.
in the App delegate, my app opens the Sqlite database, puts some of the data into a locations object with four attributes, and then makes an array of those objects.
the first view controller lets the user decide which county ( one of twelve or so) the user wishes to view, and then the last view controller uses a loop to add the selected locations to a mapView.
How should I modify my app in the previous paragraph so that it uses CoreData? Can you point me to any resources that I can use to achieve this (preferably not the Dev Center's CoreData tutorial; overly confusing and more complicated than i need right now)? or do I have to make an entirely new project in xcode and start from scratch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对间接的武断表示歉意,但是......
首先,我想说你的断言,“运行仪器后,我认为核心数据听起来是一个更好的方法......”是有缺陷的。在应用程序中查找错误和性能问题并不自动意味着不同的方法更好 - 它可能只意味着您需要修复内存泄漏和/或根据分析显示的内容调整您的方法。
您所说的仍然需要使用现有 SQLite 数据库的事实并不自动排除使用 Core Data,但它确实使您的最终解决方案显着更加复杂。如果您可以完全使用 Core Data 或 SQLite,那么这将是您最好的选择。
另外,Core Data 并不是初学者(或者,我认为,甚至是中级)Cocoa 技术。它需要大量的先决知识才能完成非常基本的事情以外的任何事情,而不会在不可避免的问题出现时毫无希望地迷失方向。如果您现在压力太大而无法花时间阅读文档并研究技术,那么您最好只使用现有解决方案来解决问题。
...并且您现有的解决方案(直接使用 SQLite)最基本的没有任何问题。更好的问题是关于您当前方法所遇到的具体问题。
也就是说,如果您想要调整现有的基于 iOS 的解决方案来使用 Core Data,那么您可能会比针对 Mac OS 更容易。创建一个基本的基于核心数据的 iPhone 应用程序项目并查看代码。构建核心数据堆栈的代码一目了然。唯一要记住的另一件事是添加一个 xcdatamodel 文件,就像在空项目中找到的文件一样。如果您已经足够了解 SQLite 库的接口,那么您应该有足够的经验来清楚地了解 Core Data 在标准 iOS 应用程序中的使用方式。
Apologies for the indirect pontification, but ...
First, I'd say your assertion, "After running instruments, I have decided that CORE DATA sounds like a better way to go..." is flawed. Finding bugs and performance issues in your app doesn't automatically mean a different approach is better - it may only mean you need to fix your memory leaks and/or adjust your approach based on what your profiling shows.
The fact you say you still need to use an existing SQLite database doesn't automatically preclude using Core Data, but it does make your end solution significantly more complicated. If you can possibly get away with using either Core Data or SQLite entirely, that would by far be your best bet.
Also, Core Data is not a beginner (or, I'd argue, even an intermediate) Cocoa technology. It requires significant prerequisite knowledge to do anything more than very basic stuff without becoming hopelessly lost when the inevitable problem arrises. If you're too pressed to take the time to read the documentation and research the technology for now, you're probably better off just fixing the problems with your existing solution.
... and there's nothing wrong with your existing solution (using SQLite directly) at its most basic. The better question(s) to ask is (are) about the specific problems you're having with your current approach.
That said, if you want to adapt an existing iOS-based solution to use Core Data, you'll likely have an easier time of it than if you were targeting Mac OS. Create a basic Core-Data-Based iPhone app project and look at the code. The code to build the Core Data stack is in plain sight. The only other thing to remember is to add an xcdatamodel file like the one found in the empty project. If you've gotten far enough to interface with the SQLite library, you should have enough experience to see clearly how Core Data is used in a standard iOS app.
转换为核心数据所需执行的步骤:
- 开发数据模型(可能在 xcode 中以图形方式)
- 采用您的代码来使用核心数据,即重写该模块
- 读取你的旧数据并将其放入核心数据
如果你已经使用 sqllite 运行它,我会坚持使用它。如果它使用了太多的内存,则可能是您的一个错误,或者您加载到内存中的数据分区错误,并且您想以一种或另一种方式解决这个问题 - 尽管核心数据使它可能更容易获得没错。
The steps you need to go to convert to Core Data:
- Develop data model (maybe graphically in xcode)
- Adopt your code to use core data instead, i.e. rewrite that module
- Read your old data and put it into core data
If you have it already running with sqllite, I'd stick with it. If it uses much too many memory, it's probably a bug on your part, or bad partitioning of the data you load into memory, and you want to address this one way or the other - although Core Data makes it maybe a bit easier to get it right.