Although Apple has touted the perfect cooperation between iCloud and CoreData, before iOS7, synchronizing CoreData data with iCloud was a nightmare. Apple itself admitted to the many bugs and instabilities before, which forced Apple to come out again and say that their engineers have fixed the bugs in iOS7 and enhanced the experience, balabala. The key is that for programmers, integrating iCloud into CoreData has become extremely simple. Apple's official documentation has clearly described the configuration work, which can be simply summarized into three steps: Create an App ID in iTunes Connect, find the Capabilities tab of your project in Xcode and enable the iCloud option. This will create a default iCloud container for you with a name in the format "com.XXX.yourAppID" When adding NSPersistentStore, pass a name for the persistent storage to the options parameter. You can just give it a name yourself. The sample code is as follows:
Register for NSPersistentStoreCoordinatorStoresWillChangeNotification, NSPersistentStoreCoordinatorStoresDidChangeNotification, and NSPersistentStoreDidImportUbiquitousContentChangesNotification so that you can process the data after receiving the notification. *** Use the addObserverForName:object:queue:usingBlock: method of NSNotificationCenter to make the logic clearer and the code more compact. ***Post the code for implementing persistentStoreCoordinator in Swift:
Of course, you can also use the lazy keyword to implement lazy loading of persistentStoreCoordinator properties. Someone has already abstracted the entire logic of CoreData integration with iCloud, such as iCloudCoreDataStack. There is no need to use third-party libraries that claim to make it easier to use CoreData and iCloud, because Apple has made it extremely simple in iOS7. However, when Xcode6 and iOS8 came out, pitfalls appeared one after another. First, iCloud Drive conflicts with the previous iCloud. If you upgrade, please upgrade iCloud Drive thoroughly and let all test machines upgrade iCloud Drive. Then, after opening the iCloud tab in the Capabilities tab in Xcode6, the following scene is simply amazing: How should I choose? ! I can only say that it is correct to choose according to the picture above. By the way, the default container name format of iCloud has become "iCloud.com.yourname.yourAppID", which is actually not quite accurate. The official name is "iCloud.$(CFBundleIdentifier)", and the variable referred to by the dollar sign is the "Bundle Identifier" value in the Identity column of General. In addition, the "Key-value storage" and "CloudKit" options can be selected or not, but "iCloud Documents" must be checked, otherwise CoreData data cannot be synchronized. PS: CloudKit is a cloud data storage service based on iCloud launched by Apple. It provides low-cost cloud storage and can serve as a backend service to share application data through users' iCloud accounts. Next, it’s time to check whether we have successfully added the iCloud container. You can try to get the URL of the container in the applicationDidFinishLaunchingWithOptions method to determine this:
If "iCloud Documents" is not checked in the iCloud of the Capabilities tab, the "URLForUbiquityContainerIdentifier" method will always return nil. Check out the discussion on this topic on the Apple Developer Forums PS: The official documentation does not recommend using the URLForUbiquityContainerIdentifier method in the main thread, because it may take a long time to return the URL and block the main thread. This is just for testing. However, Apple's official document is very detailed about whether iCloud is really working properly with CoreData: Using the iCloud Debugging Tools When I excitedly opened the debug navigator in Xcode and clicked iCloud on the left to check the status, I was stunned by what I saw: "iCloud Usage" tells me that the status is unavailable, but the Using local storage in the log in the lower right corner has changed from 1 to 0, which proves that my APP (HardChoice) has transferred from using the local persistent storage of CoreData to using the "iCloud-enabled" persistent storage. The bar chart in "Transfer Activity" even shows that data has been downloaded from iCloud. This should actually be a bug in Xcode6, and someone has discussed it in the Apple Developer Forum. According to my test, "iCloud Usage" will not appear when "Key-value storage" is checked or debugging on the simulator. Even if "iCloud Usage" appears, the status is always Disabled, and "Transfer Activity" is not very sensitive. The only thing I can trust is the CoreData log. But we can check the "iCloud Usage" of "My Mac" instead of the "iCloud Usage" of iPhone: In the "Documents" column, I can see that I have synchronized data between two devices. "Mobile" is followed by my device number. Expanding the data shows a more detailed synchronization record: Although the data synchronization records of iCloud and CoreData can be seen through "My Mac", the display of "Documents" in Xcode6.1.1 is not normal. Although the display problem of "Documents" has been fixed in the latest Xcode6.2beta version, various bugs of "iCloud Usage" still exist. ***, make sure the network is normal. When I was doing internship at Chinasoft for a month, the network was very poor, or iCloud was blocked, and I couldn't debug successfully. Here is a test picture of HardChoice synchronization being successful. Since I wrote this demo in Swift, those who like to use Swift can directly paste my source code: |
<<: Why did Baidu Cloud OS, which already has tens of millions of users, suspend updates?
>>: How was your App Store last night?
It has been exactly one month from November 6th t...
During the 2014 Shanghai Mobile Asia Communication...
In a few months, the typhoon season will come one...
Regarding event operations , this article summari...
Compiled by Zhou Shuyi and Wang Xiang Crows can c...
A 60-year-old man took a medical examination form...
The Pew Research Company released the "2014 ...
We can get a glimpse of the situation from a set ...
On December 19, BYD held the "More Than Fast...
[[404375]] Although iOS has reached its 15th vers...
[[141390]] This tutorial has been updated for Swi...
Recently, SERES Group issued an announcement stat...
Global growth is the only way for a brand. This a...
The nonprofit Coalition for App Fairness wants to...
The Chinese Valentine's Day is coming soon. H...