This is my first stack overflow question, so please excuse me if I break any etiquette. I am also pretty new to creating Objective-C / app.
I am following the CS193P Stanford course, particularly CoreData lectures / demos. In Paul Hegarty’s Photomania app, he starts by presenting a table and populating the data in the background without interrupting the flow of the user interface. I am creating an application that lists enterprises in a local area (from an api that returns JSON data).
I created the categories according to the photo / photographer classes of Paul. Creating the classes themselves is not a problem where they are created.
A simplified data structure:
- Section
- Sub-section
- business
- business
- business
- business
- business
- business
UIViewController , ( , , ). / URL- UIManagedDocument, . , , .
, Paul fetchFlickrDataIntoDocument:
-(void)refreshBusinessesInDocument:(UIManagedDocument *)document
{
dispatch_queue_t refreshBusinessQ = dispatch_queue_create("Refresh Business Listing", NULL);
dispatch_async(refreshBusinessQ, ^{
myFunctions *myFunctions = [[myFunctions alloc] init];
NSArray *businesses = [myFunctions arrayOfBusinesses];
[document.managedObjectContext performBlock:^{
for (NSDictionary *businessData in businesses) {
[Business businessWithJSONInfo:businessData inManageObjectContext:document.managedObjectContext];
}
[document saveToURL:document.fileURL
forSaveOperation:UIDocumentSaveForOverwriting
completionHandler:^(BOOL success){
if (!success) {
NSLog(@"Document save failed");
}
}];
NSLog(@"Inserted Businesses");
}];
});
dispatch_release(refreshBusinessQ);
}
[myFunctions arrayOfBusinesses] JSON NSArray, -.
NSLog . , 0,006 , . 2 .
:
@implementation ManagedDocumentHelper
+(void)openDocument:(NSString *)documentName UsingBlock:(completion_block_t)completionBlock
{
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:documentName];
UIManagedDocument *doc = [managedDocumentDictionary objectForKey:documentName];
if (!doc)
{
doc = [[UIManagedDocument alloc] initWithFileURL:url];
[managedDocumentDictionary setObject:doc forKey:documentName];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]])
{
[doc openWithCompletionHandler:^(BOOL success)
{
completionBlock(doc);
} ];
}
else
{
[doc saveToURL:url
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success)
{
completionBlock(doc);
}];
}
}
viewDidLoad:
if (!self.lgtbDatabase) {
[ManagedDocumentHelper openDocument:@"DefaultLGTBDatabase" UsingBlock:^(UIManagedDocument *document){
[self useDocument:document];
}];
}
useDocument self.document .
, , , , , .
. , . - , , !
EDIT:
. , , - , , , ? , , , , , .