Database Reference
In-Depth Information
going to be consuming the
NSURL
from more than one potential entry point,
we abstract away the handling into a
-consumeIncomingFileURL:
method. Thus, the
-contextInitialized
just needs to check whether there is an
NSURL
to consume and
hand it off. Since
-consumeIncomingFileURL:
returns a pass or fail, we can add a
logic check here to help capture failures during development.
The final change is to handle the consumption of the
NSURL
. We have already
defined the method as
-consumeIncomingFileURL:
.
- (
void
)consumeIncomingFileURL:(NSURL*)url;
{
NSData *data = [NSData dataWithContentsOfURL:url];
PPRImportOperation *op = [[PPRImportOperation alloc] initWithData:data];
[op setMainContext:[self managedObjectContext]];
[op setCompletionBlock:^(BOOL success, NSError *error) {
if
(success) {
//Clear visual feedback
}
else
{
//Present an error to the user
}
}];
[[NSOperationQueue mainQueue] addOperation:op];
//Give visual feedback of the import
}
To consume the
NSURL
, we first load it into an
NSData
object. We can then pass
the
NSData
instance off to a newly created
PPRImportOperation
. Once the operation
is complete, we will display something graphically to the user and kick off
the operation. The completion block for the operation checks to see whether
there is an error and then reports the error or dismisses the graphical status.
PPRImportOperation
Our
PPRImportOperation
has a number of similarities to the
PPRExportOperation
.
However, there is also a bit more complexity, which we'll examine now.
NSManagedObjectContext *localMOC = nil;
NSPersistentStoreCoordinator *psc = nil;
localMOC = [[NSManagedObjectContext alloc] init];
psc = [[self mainContext] persistentStoreCoordinator];
[localMOC setPersistentStoreCoordinator:psc];
As with our
PPRExportOperation
, we start off with the
-main
method. The first thing
we want to do in the
-main
is construct a local
NSManagedObjectContext
.