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.
Related
I've noticed that the method 'hashcode' for both NSMutableDictionary and NSMutableArray is giving me the hashvalue to be the number of keys irrespective of the value for NSMutableDictionary and number of objects in case of NSMutableArray.
Basically, I want to detect the change in NSMutableDictionary. My dictionary contains key/value pairs as string/NSMutableArray. And I'want to detect the change in dictionary if an item is added/deleted from any of its values.
In case if I go for calculating hash for NSMutableArray which are the values in my dictionary, it's not feasible since there's also a possibility of a different value added and existing deleted in which case simply giving me the number of items are going to remain same but the hash should be different.
Which is the best way to handle such change in dictionary?
You can retain the original dictionary an test if it is equal to the new one
isEqualToDictionary:
Returns a Boolean
value that indicates whether the
contents of the receiving dictionary
are equal to the contents of another
given dictionary.
- (BOOL)isEqualToDictionary:(NSDictionary*)otherDictionary
Docs here
I have a set of data inside a dictionary and inside
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
method I want to fetch data from this dictionary. How can we fetch objects for each indexPath.row.
One way, I could think of is getting keys array and then fetch the object from this array based on indexPath.row and then use this key to fetch the dictionary object.
Please suggest.
dictionary is not an ordered collection like array, so the method you've suggested is probably the way to go... Depends on how do you want the data to be presented (sorted by keys/values).
It's not a good practice, because the order of the keys of a dictionary is not defined.
If your data must be a dictionary, then you must provide an explicit information that maps keys of the dictionary and the index path of the table.
For example, you can have another array in which the keys of the dictionary in sorted in the same order by which you would see your objects in the table.
How are your items sorted in the table? Is the order important? There isn't really an easy answer to this question. Are you sure you need to keep a dictionary of your data? The way I see, you have the following options:
call [dictionary allValues] to get an array of all values. You can then sort it the way you want and return the item at index indexPath.row. The draw back is you need to sort the array for each row which is not ideal if you have a lot of items. It might make your scrolling choppy.
keep a copy of a sorted array version of your dictionary and make sure to update it any time your dictionary is updated. This is better suited for a large data set that does not change often. You could wrap both your dictionary and array in a class that will take care of keeping the two in sync.
You'll need to weigh the drawbacks depending on what your data is like.
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.
I'm using a dictionary and an array to store the section title for a table view, along with the number of rows in each. I'm adding to the array and the dictionary in a loop of the records from my database in mu populateDataSource method.
I've got two dates from three records in my database. 2010-11-05 and 2010-11-07
I'm not sure if its because the table view events need to start at zero and the dictionary / array start at one ?
Or maybe my sectionTitles addObject, is added values each time, rather than adding unique dates. If so how i can search or only allow unique values ?
Any ideas ?
Arrays have a starting index of 0, but dictionaries don't essentially work that way. What is the purpose of the dictionary in this case? To get an object from the dictionary you give it a key. If you're going through a loop and not getting the expected result, maybe the dictionary isn't getting the key that it should be.
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.