I need to temporarily store the content of a NSManagedObject into a dictionary. Because core data has its own memory management procedures, I don't want to keep any strong pointers to the NSManagedObject's fields, only the values are of interest at this point (values are passed between view controllers, the MOCs are different). I can't create weak pointers either because I want to control when the memory reclaim is done.
I tried a few things, all failed or did not fit the purpose.
a duplicate [[myNSMO alloc] initWithEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:myNSMO.managedObjectContext] insertIntoManagedObjectContext:nil];
It's working, but does not fit into my app design (without getting into details)
generate a NSDictionary from the NSManagedObject, using [myNSMO dictionaryWithValuesForKeys:<#(NSArray *)#>]. That's not ok because it returns a dictionary with the addresses of the NSManagedObject fields.
create a NSDictionary populating each key-value using a copyWithZone, like this
[myDictionary setObject:[myNSMO.field copyWithZone:nil] forKey:#"Key"];
Doesn't work either, I still get the field address...
Manually enter each field with
[myDictionary setObject:[NSString stringWithFormat:#"%#",myNSMO.field ] forKey:#"Key"];
It's fine this time, I do get new memory allocation. But that's highly time consuming to code this manually...
Any chance that someone found clever way to do that? the reason option 1) did not work is because I use the dictionary as a queue. I first store a copy of the object, then pop the entry out when required. A copy of that particular dictionary entry is then returned to the asking method. The problem is that I can't create a copy of an NSManagedObject that was created using [[...] insertIntoManagedObjectContext:nil];
Any solutions?
It's safe to keep strong pointers to the fields of a managed object in most senses — relationships are special but the actual Foundation objects of dates, strings and numbers are ordinary objects that'll stay in memory if you have a strong reference.
That being said, to create a dictionary copy containing all the properties of an entity you could do something like:
NSArray *properties = [[object entity] properties];
NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionary];
for(NSAttributeDescription *attribute in properties)
{
// we want only actual attributes, not relationships
// or fetched properties
if([attribute isKindOfClass:[NSAttributeDescription class]])
{
[dictionaryRepresentation
setObject:[object valueForKey:attribute.name]
forKey:attribute.name];
}
}
So you're using the fact that managed objects expose a description of their entities which includes a list of properties, whittling those properties down to just the attributes, then using key-value coding to fetch the current value of each property and finally inserting it into the dictionary.
If for some reason you did want copies of the properties — though, as I say, there's absolutely no reason to do so — you'd copy (and autorelease if you're not using ARC) each property when inserting it into the dictionary.
Related
Question 1)
I am creating a NSMutableDictionary from a NSDictionary using the following code:
_allKeysDictionary = [receivedDictionary mutableCopy];
and I am updating the values from the _allKeysDictionary using the following code:
[_allKeysDictionary setObject:[textField text] forKey:#"field"];
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary.
I need the values of receivedDictionary to be updated.
Question 2)
I am using
(__bridge CFMutableDictionaryRef)
to keep a pointer to one of the NSDictionary in my JSON response. but when I am trying to regain the above CFMutableDictionaryRef, I am still getting NSDictionary. I don't know what is wrong. I am using the following code to regain the Dictionary from the reference
NSMutableDictionary *getPointedDict = (__bridge NSMutableDictionary*) dataRefValue;
Q1: But the method mutableCopy even says so: A copy is created! The receivedDictionary logically won't be changed.
To update (after you're done):
receivedDictionary = _allKeysDictionary;
or to be safe that no changes can be made later:
receivedDictionary = [NSDictionary dictionaryWithDictionary: _allKeysDictionary];
By the way, you can write:
_allKeysDictionary[#"field"] = textField.text;
Q2: In Obj-C (and many other languages) casting does not change the structure of the object. If it's an NSDictionary, it will always stay an NSDictionary. So the actual cast will work (i.e. the compiler won't tell you), but once you start doing things with the object that are not implemented in the original class, your app will crash or you'll get other undefined results.
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary
Of course they are not, you made a mutableCopy - that is a copy. You've now got two dictionaries with the same keys and values.
I need the values of receivedDictionary to be updated
If it is an NSDictionary you cannot do this, that is what immutable means. You can replace the reference stored in receivedDictionary to one which references your new mutable dictionary (or an immutable copy of it), but that doesn't do what you wish you have other variables storing references to the original dictionary.
However if the values in the dictionary are themselves mutable then you can alter those values. E.g. if you NSDictionary contains NSMutableArray values then you can change the elements of those arrays, but you cannot change which array is associated with which key in the dictionary.
Q2: You cannot simply cast a reference to an immutable dictionary to make it into a mutable one. Casting doesn't change the referenced object in anyway, it just changes the type the compiler treats it as (and if the type is not compatible with the actual type your program breaks).
It looks like you are unsure about objects, references and mutability; probably time to do some studying.
HTH
MutableCopy & copy both do deep copy on NSDictionary. The only difference is that mutableCopy makes your new dict mutable.
When you do deep copy, you are copying the value, not the reference, which mean whatever thing you do on your new dictionary, it won't affect the old value.
Plus, your original dict is immutable, even if you copy the reference, its value won't change.
Recently i created a post about clearing dictionary formed like key->value(NSArray).
I have 2 NSDictionary, first with data, second should contain all keys from first, but empty data for that key. Data will be added/removed later. This is how i create copy from main data dict (self.viewModel.releaseDict):
_releaseFormsDictCopy = [NSMutableDictionary dictionaryWithDictionary:self.viewModel.releaseDict];
When i checked, that objects (2 NSDictionaries) have different memory.
However, when i "clear" copy with method:
-(void)clearAllArraysInsideKeys{
for(id key in self) {
NSMutableArray *arr = [self objectForKey:key];
[arr removeAllObjects];
}
}
It appears that it flush all data from both dictionaries, not only copy. How to prevent clearing first NSDictionary?
Your dictionaries may be different instances, but their contents are still the same instances - you have created a shallow (mutable) copy. From your description what you want is a deep copy, where the dictionaries are different instances and each of the arrays they contain as the values are also different instances.
Simple option is to deep copy only that level. Create an empty NSMutableDictionary and then enumerate self.viewModel.releaseDict and add each key with a mutableCopy of the value.
This will be a 'deep-ish' copy of the dictionary, containing a 'shallow' copy of each array it contains.
I created a simple database and put in NSUserDefaults. My database is NSMutableArray which has dictionaries and arrays inside in it. When I create NSMutableArray from NSUSerDefaults I can't add any objects to my mutable objects inside my NSMutableArray. Here is my code:
NSMutableArray *arrayOne = [NSMutableArray arrayWithContentsOfFile:[self createEditableCopyOfIfNeededWithFileName:#"Form.plist"]];
NSUserDefaults *ayarlar = [NSUserDefaults standardUserDefaults];
[ayarlar setObject:arrayOne forKey:#"form"];
NSMutableArray *arrayTwo = [NSMutableArray arrayWithArray:[[ayarlar objectForKey:#"form"] mutableCopy]];
[[[arrayTwo objectAtIndex:0] objectForKey:#"itemlar"] addObject:#"hop"];
And here is the error:
'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
How can I make this work? Thank you everyone.
NSUserDefaults is not the right place to store your data. First of all it should only be used for very small amounts of data, such as settings. And secondly it always returns immutable objects, even if you set mutable ones. Making a mutable copy of your first array doesn’t help because only the array will be mutable. Everything that is inside that array isn’t touched by the mutableCopy method and stay immutable.
You should use the NSPropertyListSerialization class to read and write your data from a file. On reading you can pass options controlling the mutability of the read objects. There you will want to pass NSPropertyListMutableContainers or NSPropertyListMutableContainersAndLeaves.
With the first all your containers (arrays and dictionaries that is) will be mutable. With the latter also the leaves (that is NSString and NSData objects) will be mutable.
Depending on how big your data set can get you probably should use a real database or Core Data instead.
NSUserDefaults never returns mutable objects.
Your code is performing every way of creating a mutable array you can think of (i.e. you're creating a mutable copy of something you just created a mutable copy of), but, you're only dealing with the root container item - not the inner / leaf items. So, when you do objectForKey:#"itemlar"] on your mutable array, you're getting an immutable object back.
To make it work, you'll need to write your own method that iterates and recurses through the array creating mutable copies at all levels.
Alternatively, you could look at a 3rd party option like this which digs under the hood of NSUserDefaults to generate mutable containers.
NSUserDefaults, and property lists in general, do not record mutability. When an object is re-created from the file it can be constructed either as a mutable or immutable object (for types which have the option, such as arrays). Unfortunately NSUserDefaults doesn't give you an API call to obtain an immutable object directly.
Two options you have are (a) create a mutable copy of the object returned by NSUserDefaults or (b) store the object yourself as a property list in a separate file - that way you can read it back as mutable directly.
For (b) read Apple's docs - it shows how mutability is handled.
You can directly do by using method insertObject.
In place of
[[[arrayTwo objectAtIndex:0] objectForKey:#"itemlar"] addObject:#"hop"];
use this,
[arrayTwo insertObject:#"hop" atIndex:0];
This will work for as i have tested it's also working finr after that you can make it as immutable object as NSARRAY and save it to NSUSERDEFAULTS.
I have a GameData entity which is meant to store an array of strings. So I made a 'Value' Entity which has a value string attribute and made a many-to-many relationship between the two entities.
To save the data I use the following code:
//Save values
NSMutableSet* values = [[NSMutableSet alloc] init];
for(NSString* n in gameData.values){
NSManagedObject *val = [NSEntityDescription
insertNewObjectForEntityForName:#"Value"
inManagedObjectContext:context];
[val setValue:n forKey:#"value"];
[values addObject:val];
}
[gd setValue:values forKey:#"values"];
The gameData.values array is currently empty so the code never actually goes into the for loop...but for some reason it crashes at this line [gd setValue:values forKey:#"values"] with the following error.
-[__NSSetM managedObjectContext]: unrecognized selector sent to instance 0x1f0485d0
Where or how am I sending a managedObjectContext selector to my values NSMutableSet??
Maybe you need to check whether the type of your entity is "To Many".
I cannot comment, thats why i am creating an answer.
Why don't you create the subclass for your entities using the xcode and import their header files and use code like below
//Save values
//NSMutableSet* values = [[NSMutableSet alloc] init]; -- No Need of this
for(NSString* n in gameData.values){
Value *val = [NSEntityDescription
insertNewObjectForEntityForName:#"Value"
inManagedObjectContext:context];
[val setValue:n]; // set your string
[val setGame:gd]; // set the game relation here. you can do this, if you have
configured inverse relations. If no create inverse relationship it will be helpful.
}
//[gd setValue:values forKey:#"values"]; you don't need this.
Now just save the context. Everything should be fine. This is much more cleaner than your way. I have never ever used key value for accessing core data entity properties because, it will be confusing and error prone as you have to remember the exact spelling of the property, and it wont throw any error if you have used wrong spelling or wrong key.
i think you should look at core data programming guide
Edit: If your GameEntity stores array of strings then one to many relationship is just enough. You need many to many only if GameEntity has many Strings and Each Strings i.e. Value entity also has many GameEntity. In that case the above code slightly changes.
Instead of
[val setGame:gd];
You need to use
[val addGameObject:gd];
I've Googled and searched SO for this with no straightforward results. It seems I have a fundamental misunderstanding of the following from Apple's documentation:
An archive can store an arbitrarily complex object graph. The archive
preserves the identity of every object in the graph and all the
relationships it has with all the other objects in the graph. When
unarchived, the rebuilt object graph should, with few exceptions, be
an exact copy of the original object graph.
Assume that I have an NSMutableArray that is a collection of Person objects. Each Person object implements initWithCoder and encodeWithCoder. Further, any given Person may have an NSMutableArray of objects (similarly coding-compliant) of Task.
My understanding is that there is a way to archive, thus triggering a cascading serialization of arbitrary depth, depending on implementation of the coding protocol. So in my view controller, I have a willEnterBackground that does:
data = [NSKeyedArchiver archivedDataWithRootObject:self.people];
// persist to NSUserDefaults
and I have a viewDidLoad that does:
// read from NSUserDefaults
self.people = [NSKeyedUnarchiver unarchiveObjectWithData:data];
This all happens, but two things:
The initWithCoder and encodeWithCoder in the objects contained by the people array are never called.
Unsurprisingly, the result is that self.people is an NSMutableArray of size 0. Surprisingly, the data that is unarchived is 252 bytes long, which looks about right.
Suggestions? Hints?
Even though you implemented those 2 methods, did you declare your Person class to be in the protocol ?