Cloud key-value data storage design
A programming instance of the iCloud key-value data storage. There are two controls on and off the screen. The picture on the left is device 1. Click the "set iCloud data" button to save the control status to the iCloud server. The picture on the right shows device 2. After a few seconds, device 2 receives a change notification.
Configure the xcode Project
Create an iOS project using xcode. After the project is created, select tagets → mynotes → summary → entitlements. Here we can configure the authorization information.
Then we also need to apply settingsCodeSignature ID. Select the configuration profile for the code signature ID. Select tagets → mynotes → code signing identity
After setting, you can start encoding.
Code Implementation
First, you need to register nsubiquitouskeyvaluestoredidchangeexternallynotification and synchronize data. For the code, refer to viewdidload method of viewcontroller. M:
-( Void ) Viewdidload {[Super viewdidload]; nsubiquitouskeyvaluestore * Store =[Nsubiquitouskeyvaluestore ultultstore]; ① [[nsicationicationcenter defacenter center] ② addobserverforname: nsubiquitouskeyvaluestoredidchangeexternallynotification Object : Storequeue: nilusingblock: ^ (Nsnotification * Note) {③ // Update control status [_ Switchsound Seton: [store boolforkey: ubiquitoussoundkey]; ④ [_ switchmusic Seton: [store boolforkey: ubiquitousmusickey]; ⑤ uialertview * Alert = [[Uialertview alloc] initwithtitle: @ "iCloud change notification" message: @ "your iCloud storage data has changed" Delegate : Nilcancelbuttontitle: @ "OK" otherbuttontitles: nil, nil]; [alert show] ;}]; [store synchronize]; ⑥ // Initialize control status [_ Switchsound Seton: [store boolforkey: ubiquitoussoundkey]; 7 [_ switchmusic Seton: [store boolforkey: ubiquitousmusickey]; Response}
Save the data to the iCloud storage. The setdata code of viewcontroller. M: method:
-(Ibaction) setdata :(ID) Sender {nsubiquitouskeyvaluestore* Store =[Nsubiquitouskeyvaluestore defaultstore];//Store to iCloud[Store setbool: _ switchsound. ISON forkey: ubiquitoussoundkey]; [store setbool: _ switchmusic. ISON forkey: ubiquitousmusickey]; [store synchronize];
}
Because it is a bool value, the storage method is setbool: forkey :. Finally, do not forget to cancel the registration notification. In View Controller, release notification can be completed in the didreceivememorywarning method:
-(Void) Didreceivememorywarning {[Super didreceivememorywarning]; [nsicationicationcenter defaultcenter] removeobserver: Self];}
Because there is only one notification in this application, you can use the [nsnotificationcenter defacenter center] removeobserver: Self] statement to release all notifications without affecting other notifications, if there are other notifications, we should use this statement with caution.
We can test the code after programming. The test of this application is very troublesome and requires two real devices instead of simulators. Run the two devices and click "set iCloud data" for one of the devices. After a few seconds, the other device will receive a change notification.
From IOS network programming and cloud application best practices: Guan Dongsheng Sina Weibo @ Tony _ Guan Dongsheng