Graphics Programs Reference
In-Depth Information
Nerdfeed
now appropriately handles a Core Data SQLite file that lives in the cloud. But
there are a few more changes we can make to get things running more smoothly.
First, when the change notification is posted, the store updates its records, but
Nerdfeed
's
user interface is not immediately updated. A store should always notify controllers when
it changes the data it returned to them, so
BNRFeedStore
will post a notification
whenever it merges changes from the iCloud transaction log. In
BNRFeedStore.h
, de-
clare a new string that will serve as the notification name.
@class RSSChannel;
@class RSSItem;
extern NSString * const BNRFeedStoreUpdateNotification;
@interface BNRFeedStore : NSObject
This declares a new global variable named
BNRFeedStoreUpdateNotification
.
The
const
after the star (
*
) says that once this variable is initialized to point at an object,
it cannot be changed to point at any other object. The
extern
says that this variable is
declared in this file but will be defined elsewhere. That elsewhere is
BNRFeedStore.m
.
In
BNRFeedStore.m
, define this variable.
#import "RSSItem.h"
NSString * const BNRFeedStoreUpdateNotification = @"BNRFeedStoreUpdateNotifica-
tion";
@implementation BNRFeedStore
Whenever
BNRFeedStore
merges the changes in Core Data, it will post this notifica-
tion. The
ListViewController
will register as an observer for this notification and
update the table view when the notification is received. However, there is a small issue:
BNRFeedStore
is told to perform the merge on a background thread, so the notification
will be posted and received on a background thread as well. This will sometimes cause a
delay between the merge in Core Data and the updating of the table view.
Thus, we want to post the
BNRFeedStoreUpdateNotification
on the main
thread. In
BNRFeedStore.m
, post this notification on the main thread in
con-
tentChange:
.
- (void)contentChange:(NSNotification *)note
{
[context mergeChangesFromContextDidSaveNotification:note];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSNotification *updateNote =
[NSNotification notificationWithName:BNRFeedStoreUpdateNotification
object:nil];
[[NSNotificationCenter defaultCenter] postNotification:updateNote];