How to fast enumerate and compare objects of many NSArrays in Objective-C?
I have many different NSArray's stored in .dat files, in the Documents folder of my iPhone application, like so:
john.dat mary.dat bob.dat etc...
The number of .dat files is unknown and it will increase or decrease according to a number of factors related to the user's manipulation of the app.
What are the contents of these NSArray's stored in the.dat files? They contain NSString's. It is important to say that the countfor each NSArray my vary, like so:
john.dat Contains this array of strings: "Mon, 11 Jun 2012 04:52:06 GMT", "Tue, 12 Jun 2012 04:51:59 GMT", "Wed, 13 Jun 2012 04:52:30 GMT", "Thu, 07 Jun 2012 04:52:00 GMT", mary.dat Contains this array of strings: "Mon, 11 Jun 2012 04:52:06 GMT", "Tue, 12 Jun 2012 04:51:59 GMT", bob.dat Contains this array of strings: "Mon, 11 Jun 2012 04:52:06 GMT", "Tue, 12 Jun 2012 04:51:59 GMT", "Wed, 13 Jun 2012 04:52:30 GMT", "Thu, 07 Jun 2012 04:52:00 GMT", "Fri, 08 Jun 2012 04:51:59 GMT", "Sun, 10 Jun 2012 04:50:55 GMT"
I have to periodically verify if each NSString of these NSArray's has been modified, to notify the user of certain events. After doing a XML parsing from the Internet I'm already able to have in memory the new NSArray's corresponding to its respective .dat file previously stored in the Documents folder.
Since I have many .dat files, my question is:
How do I compare EFFICIENTLY AND AT ONCE the new NSArray's (NSArray *john, *mary, *bob, etc...) that I have in memory (as a result of this XML parsing), with the old ones stored in its corresponding john.dat, mary.dat, bob.dat, etc... files in the Documents folder and notify the user if any of the NSString's ("Mon, 11 Jun 2012 04:52:06 GMT","Tue, 12 Jun 2012 04:51:59 GMT", etc...) has been modified?
Thanks for your help!
If you're happy to change the format of your .dat file you could compute a hash of each array's contents and store that at the beginning of the file. When you get new data you can compute its hash and compare that with the hash stored in your file.
Your choice of hashing function will determine the computational efficiency, but the I/O would be minimized.
Well, if you content with storing your arrays as .dat files (with NSKeyedArchiver I'm guessing), then Martin's answer is probably best and the tried and true method of comparison
The approach I might take is to store the NSStrings in NSDictionarys instead of NSArrays. Then you could set an observer on its values, so when an string is pulled from the XML file you compare, replace if necessary and the observer will be called so you can notify the user (see NSNotificationCenter and KVO if not already familiar).
It may also be beneficial (usually is) to store your strings in CoreData as opposed to .dat files, then you could configure an NSFetchedResultsController to keep an eye on your data and do the above when new data is fetched.