Database Reference
In-Depth Information
As you can see we are now using our AppDelegate #define that we added in our iCloudTestApp-
Prefix.pch file. This makes the call to updateKeyValueStoreKey:withObject a lot easier to read.
The last thing we need to do is update our btnClosePressed: method. We want to call the
syncKVStore method on our Application Delegate after the dismissal is complete. But, we only want
to call this if a change actually took place because we don't want to be flooding the store with sync
calls when nothing has changed. The method should now look like this:
#pragma mark - Button Methods
-(IBAction)btnClosePressed:(id)sender {
[self updateAllDefaults];
[self dismissViewControllerAnimated:YES completion:^{
if(_isChanged)
[AppDelegate syncKVStore];
}];
}
This completes the iCloud Key-Value Store implementation, but we still have one small
problem. Because the Key-Value Store updates can happen at any time while the app is
running, we should also configure our UI to handle that. This is an easy task to handle
with NSNotificationCenter . Similar to the way we added ourselves as listeners for the
NSUbiquitousKeyValueStoreDidChangeExternallyNotification message we can add ourselves as
listeners to a message that we will actually create.
We will start by adding another constant declaration in our Constants.h file:
extern NSString * const UIShouldRefresh;
Also, don't forget to add the implementation in the Constants.m file:
NSString * const UIShouldRefresh = @"UI_SHOULD_REFRESH";
Now let's go back to the CTAppDelegate.m file and modify our storeDidChange: method by adding
the following invocation inside the if statement we use to check if there is a change we care about.
[[NSNotificationCenter defaultCenter] postNotificationName:UIShouldRefresh object:nil];
Our final step is to set up our Main View Controller and Settings View Controller to listen for this
message and respond. Inside each of these view controllers we will add the following line to the
viewWillAppear: method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI:)
name:UIShouldRefresh object:nil];
When you set a class as an observer for a notification it is best practice to remove yourself as
an observer when you don't need to be. It is important to remember that even though the view
controller isn't on-screen, it doesn't mean that it has been removed from memory and isn't
accessible. In our case we only care about listening for the message UIShouldRefresh when the
view controller is actually being displayed. Because of that, let's implement the viewDidDisappear:
method and remove ourselves as an observer.
 
Search WWH ::




Custom Search