Source: Informit Translation Source: Cocoachina
Core data is a framework that Apple has developed for Mac and iOS, and is used primarily for the storage. For many developers, Core data is easier to start with, but hard to master, and without the right learning method, it's hard to really understand it, let alone master it. Many developers often make mistakes in this area, and this article lists some of the common errors that developers use with core data during iOS development, and analyzes how to avoid these errors.
1. Not understanding key terminology
For iOS developers, using core data is a must-have skill. Without it, many apps will not exist. When searching around the Internet for core Data learning tutorials, you can easily be intimidated by a variety of terminology. In fact, most of the learning tutorials assume that you already know these terms, and if you don't understand them, you're going to get into confusion. So first you need to know the key terminology. There is a cheat sheet that can be used in the process of learning core data, this cheat sheet shows the key phrases:
In the future, you will encounter more terms, but these are the basic parts that beginners need to understand.
2. Completely ignoring core Data
When a technology is "famous" for being difficult to learn, you may overlook it, especially if you don't have enough time to get the app out.
One of the most common alternatives to Core data storage app data is the use of XML attribute lists, although the list of attributes can make your work today easier, but they will then come back to bite you. Whenever you edit a property list, the changes that occur are atomic. This means that even small changes require the entire file to be loaded into memory, and then the entire file will be written back to the hard disk when it is saved.
As the amount of data grows, the app will become more and more slow. But if you're using core data based on your SQLite database, these performance issues won't bother you. This keeps the memory consumption low to ensure the app responds quickly and prevents the app from collapsing due to excessive memory pressure. Essentially, the reason that Core data is more extensible than the attribute class table is that it supports persistent storage using a database. Scalability is not the only advantage of core data, and it is powerful to use relationships to organize data into a physical structure. For example, consider using the following entities to represent a task:
The task entity contains a name and a Subtask_name property. When you create a management object from a task entity, it will have a name and a Subtask_name property.
Without dependency, this data model supports only one subtask, and now considers the following entities:
A line with double arrows indicates that the task entity can correspond to multiple subtask entity relationships, which means that a task can have multiple subtasks, not to mention that the parent task involved can also be included in the inverse relationship. This flexibility is not only convenient, but also saves the database space, because the parent task name only needs to be stored once. What if you want to take a step further and have subtasks for subtasks? Think about rebuilding the following task entities:
The model now supports the sub-task of wireless depth because the task entity is associated with itself! The scalability and flexibility of Core data is only a small part of its benefits. Core data doesn't just take advantage of a relational database, and you don't have to write any SQL statements to use it. Core data takes responsibility for you and automatically optimizes the generated SQL statements for you.
I haven't been delving into other value aspects of core data, such as model versioning, migration, validation, change management, icloud synchronization, and so on. If there's any iOS framework that's worth your time, it's core Data.
3. Do not use model versioning and migration
If you have already edited a management object model, you may have made the following error:
"The model previously used to open the store was incompatible with the model used to create the store"
When you create a data persistence store, it is based on a specific management object model. If the structure of the model changes, persistent storage must be updated to match. If you do not do this, the store will be incompatible and cannot be opened. If the user is using a store that is based on your model without using version control, then the app is doomed to crash.
To ensure that the model migration process is working properly, you need to make sure that you have added the model version very carefully before you edit the model.
Note: Some changes, such as property defaults, validation rules, and get request templates, can be simplified.
4. Excessive use of version control and migration
Once developers understand the simplicity of maintaining the version of the managed object model, some developers will inevitably be overly used. This results in an overly complex version history, which only slows down the migration of the model if each change is added to the version.
Before you release the core Data app to the App Store, you can ignore version control and edit the model as you like. To avoid the "the store is incompatible" error, you can simply remove the app from your development device and run it again in Xcode. By deploying a new persistence store with the updated model, you can resolve the crash issue. Once you publish model version 1 to the App Store, all of your users will have version 1 persistent storage. At this point, if you update the model, you must add a new version. We assume that your users are using Model version 1. When developing a newer version of the app, you've added model versions 2, 3 and 4. Use the following tips to reduce the version history without releasing the model versions 2, 3,4 ...
Delete the contents of Model 2
Copy Model 4 content to model 2
Set Model 2 to the current model
Delete Model 4
Of course, you need to consider how the entities in Model 1 map to the more important model 2, especially if you're not using lightweight migrations. For a more detailed version of model versioning and migration, you can view the complete chapter "Learning Core Data for IOS".
5. Leave everything in memory
You're focused on features and functionality, so it's easy to forget about less glamorous topics like keeping your memory low. Some developers will be rushing to publish applications before performing a performance test, especially if deadlines are required. But fortunately we still have some measures to help you keep your memory consumption low.
When you manage objects, you can use the Manage objects context in memory. Once you have finished managing the objects, you should remove them by calling one of the following Nsmanagedobjectcontext instance methods.
Removes all management objects from the context by resetting.
Use Refreshobject:mergechanges and pass in the parameter no to remove a specific object from the context.
Use either of these methods to ensure that unused objects do not waste space. To increase the visibility of the number of objects in the context, you can log [[context Registeredobjects] count] results to facilitate debugging in the console.
6. Design a low-quality managed Object Model
If you store photos, audio, or video, you need to be very careful with your model design. The key point to remember is that when you bring the managed object into context, you are bringing all the data into memory. For example, if a managed object has an image property that stores a large picture, and a table view uses it to create many entity objects and populate the cells, app performance is affected. Immediately you use a controller to get results, you still need to load a lot of high-rate pictures at once, this operation will not be executed immediately. To solve this problem, attributes that hold a large number of objects should be split into an associated entity. In this way, a large number of objects can be persisted for storage. If you need to show photos in table view, you should use auto-generated thumbnails instead.
7. Do not load data in advance
When you load the model into an updated app, be careful not to accidentally load a default data store based on the old model. If you do this, it may cause crashes for some users when they run the app. This threat can fundamentally prevent developers from loading a default data store.
If the default data is included in the app, the app is easier to learn and use. The easier it is to use a program, the more likely it is that the user will continue to use it. The longer the user is using an app, the greater the chance that the user will spread it, and finally the potential sales to the app. To avoid a crash in the event of an update that provides default data, you need a good test strategy.
In addition, you need to have a deep and accurate understanding of what model versions and storage you want to publish to the App Store. You should deploy an unchanged App Store app version to your device, add data, and thoroughly test the upgrade process.
8. Use only a single contexts
The implementation of Core data requires at least one context to operate on the main thread. The user interface also needs to run on the main thread, so any action that slows the main thread slows the program's responsiveness. While it's easy to use a context, performance problems can occur quietly unless your data settings are very small. For example, if you want to generate data thumbnails, or import some data, the app will block in these processes.
Since iOS 5, managing multiple context has become very easy. Now you can configure a context level and run some contexts in the foreground and background. You can implement background saving by configuring background context as the parent of the foreground context. By configuring background context as a subclass of the foreground context, you can import the context as an import object to automatically update the user interface.
9. Don't understand the limitations of icloud integration
After IOS 7 was released, the implementation of Core data integration with icloud became simpler. One of the key limitations of icloud is that its data is constrained to an icloud account. Because icloud accounts are intertwined with all aspects of the user's device, sharing an icloud account is impractical and not recommended. This means that icloud cannot be used for sharing. For example, it is not currently possible for icloud to assume that a husband and wife want to list items on the same shopping list.
In addition to account restrictions, icloud does not support ordered relationships and restricts your lightweight model migrations. Think outside of this circle, and if you're interested in collecting and analyzing statistics that your app uses, you might consider using Backend-as-a-service (BaaS).
10. Integrate icloud without considering existing customer data
In iOS 7, icloud integrates core data much easier, and many developers have the confidence to support it in the app, which has previously been used to host precious user data that is not stable. This has led to a lot of existing apps that are stored locally, such as my own ' Teamwork ' app.
One of the important simplifications of icloud in IOS 7 is the introduction of the fallback store, which allows seamless transitions between icloud accounts and icloud documents and data. Users can use icloud-enabled apps, even if they don't have any network connectivity and integrate data into icloud when they have available networks.
While this is a bit of a mystery, local storage scenarios for storing user data should be forgotten, based on apps developed prior to iOS 7.
If you only turn on icloud, you'll be using a different store, and you'll need to merge the user's local data into icloud. Before you try to integrate user data into icloud, you need to check the following points:
Does the user register for icloud?
Do users want to use icloud in the app?
Do users want to merge local data into icloud?
If there is a "no" in the answer above, then the app should be able to handle different answers in the future. If your answer is "Yes," then you need to manage the process of migrating users ' on-premises data to icloud. Things get interesting when you have local data on multiple devices. If so, you will need to consider the deduplication strategy.
Summarize
If you have an iOS framework that's worth your time, it's core Data. If you are interested in it, consider my new book –learning Core Data for IOS. This is an iOS 7-based book that takes you through the entire core data tutorial. A summary of the book can be viewed here.
10 errors to avoid when using core data in the iOS development process