Loading data objects into an NSArray causes a painfully slow startup - iphone

I have a Core Data database of about 500 objects. These objects are 'cards' that will be viewed and modified by the user. When a user modifies the card, an attribute called 'groupNumber' will change.
The order of these cards in each group is very important and is determined by the user. I load the database objects into an array. When a user makes a change, I save the order of the array into a plist using the 'title' attribute.
My problem comes when the app is relaunched. I need to load the group array in the order it was saved in. But when I use the plist to do a fetch request, it is painfully slow.
The slow code is:
// get array from plist sorted by 'title'
NSMutableArray *group1Temp = [plistData objectForKey:#"group1ArrayData"];
for (int i = 0; i < [group1Temp count]; i++) {
// set predicate to 'title' attribute
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"title == %#", [group1Temp objectAtIndex:i]];
// load filtered database object into temp array
NSArray *temp = [self.vocabDeckArray filteredArrayUsingPredicate:predicate];
[self.group1Array addObjectsFromArray:temp];
}
When this executes 500 times, it is just too slow. If I could save an NSArray of the database objects themselves into a plist, then I wouldn't need to do a predicate search, but it seems I can't do that.
I'm thinking my entire approach to this has been wrong. How can I save/load the order of an array of database objects in a faster way? Is loading database objects into an array itself bad practice?
Thanks for any help!

Traversing the entire vocabDeckArray and filtering it once for each object in group1Temp is very inefficient. There are any number of ways you could rebuild this sorted data set in less than O(n^2) time.
One easy option might be to store a dictionary with the object's titles as keys and their position in the array as values. That way you can construct an array of known length and put every object in vocabDeckArray into the correct position in a single pass (get first object from vocabDeckArray, lookup where in belongs in group1Array from the dictionary, insert into group1Array, move on to next object). That's still not particularly fast but it seems like a minimal change to your current behavior.
In addition consider the number of method calls within your loop. self.vocabDeckArray and self.group1Array are method calls which you make on every iteration of your loop even though they always return the same objects. Keeping local variables which reference those objects instead would save you the overhead of 2 method calls on every iteration.

Related

How can i save the order of an NSMutableArray

I have an NSMutableArray and I change the order of the contents of it. How can I save the order of it in core data without using a relation ship, or should I use NSUserDefaults?
Why not have a second array that is a copy of the first one at the time that you want to preserve its order? It need not be a mutable array - use [myArray immutableCopy]
If you don't want to copy all the items in the array for memory reasons, perhaps store the location as an NSInteger? Have the first array contain the items, and the second array contains indexes for the first array.

duplicates data while adding into table

In my app when i insert data by clicking on add button data will be inserted into database and table.record will be inserted perfectly but when i add that data into table it duplicates the database data..here is the code:
NSString *insertData=[NSString stringWithFormat:#"insert into tbl_Bills(Amount,Note,Due_On) values ('%f','%#','%#')",addAmountInDouble,val,due];
[database executeQuery:insertData];
NSLog(#"inert query: %#",insertData);
NSLog(#"value inserted");
NSString *countQuery = [NSString stringWithFormat:#"Select Bill_Id,Entry_Date,Amount,Note,Due_On from tbl_Bills"];
NSArray *countArray1 = [database executeQuery:countQuery];
NSLog(#"Query:%#",countArray1);
[BillTableArray addObjectsFromArray:countArray1];
NSLog(#"Count array %#",countQuery);
NSLog(#"table array:%#",BillTableArray);
[database close];
[table reloadData];
count array is fine but table array duplicates the value..i also initialize table array in viewDidLoad() method.
first of all you need to clear your table array before adding new objects :
[BillTableArray removeAllObjects];
after add new updated objects :
[BillTableArray addObjectsFromArray:countArray1];
This is because you are not clearing data out of BillTableArray before adding the new data into it. You are rerunning your query across the entire table - thus retrieving all of your records again, including the ones you've already set up in BillTableArray on the initial view load.
The problem is this line:
[BillTableArray addObjectsFromArray:countArray1];
BillTableArray is already loaded with data from your initial viewDidLoad call, but the query you executed retrieves all of this data from the database again, and this line is adding all of those objects to the array for a second time. So you are probably seeing duplicates of everything except the newly added items.
You probably just need to do:
BillTableArray = [countArray1 retain];
or something similar (don't forget to retain/release objects appropriately to ensure you don't lose them or leak them, not sure how BillTableArray is declared.
BillTableArray presumably contains all the data you already had in the table. When you re-query the database to get your added value, you will be returning everything, then adding it all again.
So, either
Specifically add only the new objects to BillTableArray
Change your countQuery to only return the new object
Replace all the contents of BillTableArray with countArray1
[BillTableArray addObjectsFromArray:countArray1]; adds all the objects from array countArray1. So I guess the existing objects in BillTableArray are not removed before adding the new objects. This creates duplicates. To remove duplicates, just remove all the objects of BillTableArray and then add the objects from countArray1.
Just a small note, please make sure you name the variables starting with a lower case alphabet.

Optimizing text searching using NSFetchedResultsController & Core Data

I have a tableview with a NSFetchedResultsController data source, displaying a list of names from the underlying Core Data SQLite store. I have implemented a search bar. When the first character is entered in the search bar, a fetch request in the following form is executed:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:#"name beginswith[cd] %#", searchText];
However, when the second search character is entered, I would like to filter the fetched objects of the fetchedResultsController, rather than executing another fetch request (implementing another fetch request, similar to the 1 above, results in another trip to the store which I had hoped would not occur as the results of the second fetch would just be a subset of the first). Is there anyway to filter the fetchedResultsController so that another trip to the store is avoided?
Yes, set up a search "state" and then switch your NSTableViewDatasource to point at an array that is filtered from the -fetchedObjects returned from your NSFetcResultsController.
You can then update the filter on that array as the user types more information in and it will not go back to the store. This will even allow you to filter on the first character entered and avoid even that unnecessary trip to the store.
You could always store the results of the first fetch into an array and when text in the search bar changes, filter the contents of the array with another predicate.

NSMutableArray: count is a method or a property?

I want to verify if I'm in the last cell of the UITable.
Should I use a NSInteger property in my controller and store the value of the count of the data (it's an NSMutableArray) that populates the table?
I'm asking because I don't know if count iterates over all objects of the array to count or if it only gets me a value of a count property.
What would be better ?
store the value: myCount = [myArray count]
use directly: [myArray count]
store numberOfRows: myCount = numberOfRowsInSection:[indexPath 0]
use numberOfRows directly: numberOfRowsInSection:[indexPath 0]
Use the method directly. It is almost certainly efficient enough. If you are having serious performance problems, use shark and instruments to discover why. If you see most of your time is being spent in calls to count, find a way to optimize that, like you suggested here. But don't optimize prematurely like this--you are just wasting your time even thinking about it.
Because the state of the table view can change at any time, depending on what the iPhone OS is doing at any given time, you will almost always want to pull state data from your model (from the array, in this case).

NSMutableSet vs NSMutableArray

What's the difference?
In my context, I need to be able to dynamically add to and remove objects. The user clicks on rows of a table that check on and off and thus add or remove the referenced object from the list.
A wild guess is that array has indexed items while set has no indexes?
An NSSet/NSMutableSet doesn't keep items in any particular order. An NSArray/NSMutableArray does store the items in a particular order. If you're building a table view, you should definitely use an array as your data source of choice.
Also, NSMutableSet makes sure that all objects are unique.
NSMutableArray goes well with UITableView since elements have index, so you can return [array count] to get number of table rows, or [array objectAtIndex:rowNumber] to easily associate element with row.
Also, according to the documentation, testing for object membership is faster with NSSets.
You can use sets as an alternative to arrays when the order of
elements isn’t important and performance in testing whether an object
is contained in the set is a consideration—while arrays are ordered,
testing for membership is slower than with sets.
Well there are 3 main differences. 1) Sets are unordered 2)Don't have an index & 3)Cannot have duplicate values where Arrays are ordered, indexed & can store duplicate values.