Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify which property to extract when using dictionaryFromObject() method? #18

Closed
hopiaw opened this issue Feb 16, 2015 · 9 comments
Assignees
Labels

Comments

@hopiaw
Copy link

hopiaw commented Feb 16, 2015

Hi,

When using the following method:

(id)dictionaryFromObject:(NSObject *)object;

and object inherit NSManagedObject, the dictionary returned has a lot of NSManagedObject properties that I'd like to remove (like inserted, deleted, hasChanges, faultingState etc). Is there is an easy way to do that?

Thanks

@aryaxt
Copy link
Owner

aryaxt commented Feb 16, 2015

Try using this:

[managedObjectInstanceProvider setUniqueKeys:@[@"userId"] forClass:[User class] withUpsertMode:UpsertModePurgeExistingObject];

UniqueKeys ensure that when it finds an NSManagedObject with existing unique identifier it overrides them instead of creating a new instance.

Having upsert policy set to UpsertModePurgeExistingObject deletes the existing (if it finds an existing) one and creates a brand new one, which means it triggers all your cascade delete rules

Let me know if this does what you are looking for

@hopiaw
Copy link
Author

hopiaw commented Feb 16, 2015

Thanks for the quick reply. This is not my issue I think. I'm actually setting a unique key for my class with the mode UpsertModeUpdateExistingObject. It works great.

But when I want to generate a dictionary from my object model, the dictionary returned has all the objects properties plus the ones related to any NSManagedObject class.

For example, for the following class:

class MyClass
{
    var name:String
    var age:Int  
}

I'll get a dictionary with maybe 10 key/value pairs more than the number of the object properties:

"name": ....
"age": ...
"faultingState": ....
"deleted": 0
"existingID":....
etc...

I'd like to obtain a dictionary that contains only the properties of my object. Hope it's more clear.

@aryaxt
Copy link
Owner

aryaxt commented Feb 16, 2015

Got it, I used the runtime API for all object to dictionary conversion.
Need to change it to use NSPropertyDescription for NSManagedObjects. Will make that change on the next version

@aryaxt aryaxt added the bug label Feb 16, 2015
@aryaxt aryaxt self-assigned this Feb 16, 2015
@hopiaw
Copy link
Author

hopiaw commented Feb 16, 2015

Cool, thanks a lot!

@poetmountain
Copy link

This would be really helpful for regular NSObjects as well; I'm currently running into this issue with my own NSObject models. Also, sometimes I only want to send changed properties to a PATCH request. So perhaps it would be better to be able to submit a whitelist of properties you want published, maybe via a new dictionaryFromObject: withWhiteList: method so that whitelists can be defined as-needed.

@aryaxt
Copy link
Owner

aryaxt commented Apr 6, 2015

@hopiaw @poetmountain I'm adding this feature today.
Would the following method get the job done for what you guys are trying to achieve? Guessing you could loop through all NSManagedObjects dynamically and call exclude on all, or maybe you could event add that for NSManagedObject.class

- (void)excludeMappingForClass:(Class)class withKeys:(NSArray *)keys;

@aryaxt
Copy link
Owner

aryaxt commented Apr 6, 2015

Example:

mappingProvider.excludeMappingForClass(NSManagedObjects.self, withKeys: ["faultingState", "deleted"])

@aryaxt aryaxt closed this as completed Apr 7, 2015
@poetmountain
Copy link

@aryaxt If I'm understanding this correctly, this method would apply a global whitelist per-model? I think this probably addresses hoplaw's use-case, but in a case where you're sending a partial model via PATCH, and those model properties might be different every time (e.g. Message model where user can edit a 'title' or a 'body' property individually), a class-level whitelist wouldn't work. But perhaps that sort of dynamic model filtering isn't worth putting into the lib.

@aryaxt
Copy link
Owner

aryaxt commented Apr 7, 2015

Yeah, feels like it would be easier to manually generate a dictionary, versus using the library with a whitelist properties array. The feature I added is a static mapping feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants