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

Best way to map subrequest without manual add/remove #32

Closed
k06a opened this issue May 19, 2015 · 7 comments
Closed

Best way to map subrequest without manual add/remove #32

k06a opened this issue May 19, 2015 · 7 comments
Assignees
Labels

Comments

@k06a
Copy link
Contributor

k06a commented May 19, 2015

I am using request to get places:

http://.../api/json/GetPlaces

Then I need to make request:

http://.../api/json/GetPlaceMenu?place_id=%@

I am doing this way:

NSArray *places = [FEMManagedObjectDeserializer deserializeCollectionExternalRepresentation:responseJSON usingMapping:[Place mapping] context:[NSManagedObjectContext MR_defaultContext]];

For each place I am doing request and mapping like this this:

NSArray *menus_arr = [FEMManagedObjectDeserializer deserializeCollectionExternalRepresentation:responseJSON usingMapping:[Menu mapping] context:[NSManagedObjectContext MR_defaultContext]];
NSSet *menus = [NSSet setWithArray:menus_arr];

And then I am trying to determine menus to add and to remove from place objects:

NSMutableSet *menusToAdd = [NSMutableSet set];
for (Menu *menu in menus) {
    if ([place.menus objectsPassingTest:^BOOL(Menu *obj, BOOL *stop) { return [obj.menu_id isEqual:menu.menu_id]; }].count == 0)
        [menusToAdd addObject:menu];
}
NSMutableSet *menusToRemove = [NSMutableSet set];
for (Menu *menu in place.menus) {
    if ([menus objectsPassingTest:^BOOL(Menu *obj, BOOL *stop) { return [obj.menu_id isEqual:menu.menu_id]; }].count == 0)
        [menusToRemove addObject:menu];
}

[place addMenus:menusToAdd];
[place removeMenus:menusToRemove];

Really I don't wanna use this code :) I wanna Fast and Easy Mapping. Please tell me how to avoid this code with FAM.

@k06a k06a changed the title Best way to achieve subrequest Best way to map subrequest without manual add/remove May 19, 2015
@dimazen
Copy link
Contributor

dimazen commented May 25, 2015

@k06a basically all you need is to "replace" old menus by new one with reuse of existing? Am i right?

In this case this code should help you:

FEMManagedObjectMapping *placeMapping = ...;
FEMManagedObjectMapping *menuMapping = ...;

FEMRelationship *menuRelationship = [[FEMRelationship alloc] initWithProperty:@"menuRelationshipPropertyNameForPlace" keyPath:@"keyPathForMenusInJSON" assignmentPolicy:FEMAssignmentPolicyCollectionReplace objectMapping:menuMapping];

[placeMapping addRelationship:menuRelationship];

@dimazen
Copy link
Contributor

dimazen commented May 25, 2015

@k06a
Also does http://.../api/json/GetPlaceMenu?place_id=%@ contain identifier of place or just nested menus?

@dimazen dimazen self-assigned this May 25, 2015
@k06a
Copy link
Contributor Author

k06a commented May 25, 2015

@dimazen, Menu json does not contains parent place_id.

@dimazen
Copy link
Contributor

dimazen commented May 25, 2015

@k06a ok, in this way you probably need something like that:

EMManagedObjectMapping *placeMapping = empty mapping with entity name. ;
FEMManagedObjectMapping *menuMapping = real mapping of menu;

FEMRelationship *menuRelationship = [[FEMRelationship alloc] initWithProperty:@"menuRelationshipPropertyNameForPlace" keyPath:nil assignmentPolicy:FEMAssignmentPolicyCollectionReplace objectMapping:menuMapping];

[placeMapping addRelationship:menuRelationship];

After you're done with mapping execute this code

[FEMManagedObjectDeserializer fillObject:userPlace fromExternalRepresentation:dictionaryFromNetwork usingMapping:placeMapping];

@k06a
Copy link
Contributor Author

k06a commented May 25, 2015

@dimazen thanks!

@k06a
Copy link
Contributor Author

k06a commented May 31, 2015

@dimazen I am using deserializeCollectionExternalRepresentation and I am falling on NSAssert on FEMCache.m:94:

NSAssert(
    NO,
    @"Expected container classes: NSArray, NSDictionary. Got:%@",
    NSStringFromClass([representation class])
);

I found similar problem and similar solution here: #26 But it is not working for me.

@dimazen
Copy link
Contributor

dimazen commented Jun 4, 2015

@k06a hm, can you share the JSON & mapping?

@dimazen dimazen closed this as completed Mar 1, 2016
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

2 participants