Andrea
Andrea

Reputation: 1701

Loading a Big Plist File

I have a big plist file, I have a problem, when I do:

 [NSMutableDictionary dictionaryWithContentsOfFile:docDirPath];

I must waiting for some seconds before I can use the application. Is there some solution?

Thanks

Upvotes: 2

Views: 402

Answers (2)

zaph
zaph

Reputation: 112857

Load the plist in another thread, GCD is perfect for this.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{ [self.theDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:docDirPath];   });

If the class of the method that does this will not live until the plist read is complete you will need to wrap the block in a copy to Heap.

dispatch_async(queue, [[^{ [self.theDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:docDirPath]; } copy] autorelease]);

Upvotes: 2

EricS
EricS

Reputation: 9768

If the plist is stored as a text plist, convert it to a binary plist instead. They load much faster.

The easiest way to do that is with plutil:

plutil -convert binary1 file.plist

(This assumes that it's a static plist file rather than one created on-the-fly. Within your app you can store a dictionary as binary using NSPropertyListSerialization)

Upvotes: 2

Related Questions