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

Mapping but not store #104

Open
vishaldeshai opened this issue Aug 31, 2017 · 29 comments
Open

Mapping but not store #104

vishaldeshai opened this issue Aug 31, 2017 · 29 comments

Comments

@vishaldeshai
Copy link

Hello,

I have Core Date entity and store array of data but now i want to create array of data with the use of FastEasyMapping but dont want to store inside DB.

Thanks in advance.

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

Hello, @vishaldeshai
Sorry for a delay.

FEM currently supports only mapping to either NSManagedObject or to the NSObject. Therefore it looks like that you're looking for a NSObject mapping.
In this case create your mapping by [[FEMMapping alloc] initWithObjectClass:[MyNSObject class]]

@vishaldeshai
Copy link
Author

Suppose i have array of dictionary then??

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

@vishaldeshai are you trying to map an array of dictionaries to the NSManagedObject's property?

@vishaldeshai
Copy link
Author

I have array of dictionary and i got that from web service but i dont want to store that on Core data but only need to mapp is it possible???

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

Sure. You may want to map them to the plain nsobject's that you have.

@vishaldeshai
Copy link
Author

NSObject is super class for either nsaray or nsdictionary.

so we need to iterate everydictionary and map to FEMMaping right??

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

Nope, you just need to map that exactly as you would do this for the CoreData except a different initializer for the Mapping.

Can you share sample json and a desired output?

@vishaldeshai
Copy link
Author

I have two types of Visit Like Plan and Unplan.

i am storing and mapping Plan Visit after Login but i dont have to store unplan Visit for my application. Json for Plan and plan visits are same.

So what is the desirable solution??

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

You can either provide an NSObject duplicate for the Visit or to store them in the in-memory / nested managed object context that should not be saved to the database.

@vishaldeshai
Copy link
Author

Can you elaborate more??

Json are same means format are same but values are different. :)

@dimazen
Copy link
Contributor

dimazen commented Sep 1, 2017

I can't get what you mean. If the format is the same, than you should be able to map them to the same entity Visit. What I'm proposing is remove saving of the context after mapping. in this case you can get all of the results in memory. Another way is to provide NSObject's subclass with the same properties that you have for the Visit and map json to it.

@vishaldeshai
Copy link
Author

I have already created Coredata ManagedObject Subclass.

@dimazen
Copy link
Contributor

dimazen commented Sep 2, 2017

I've made a request and can not understand what is your issue. Do you want to prevent to store response in the database?

@vishaldeshai
Copy link
Author

Yes

@dimazen
Copy link
Contributor

dimazen commented Sep 2, 2017

As I already said you should either save objects into the nested context without saving or to provide class with duplicated set of the properties, that you have on the CoreData's model. In this way you'll get an array of NSObject's subclasses.

@vishaldeshai
Copy link
Author

nested context means how??

@dimazen
Copy link
Contributor

dimazen commented Sep 2, 2017

You can read it here:
https://www.cocoanetics.com/2012/07/multi-context-coredata/

In short nested context will still be used in the same way as using plain managed object context but not saving it.

One sec:

FEMMapping *mapping = // your mapping here
FEMManagedObjectStore *store = [[FEMManagedObjectStore alloc] initWithContext:yourManagedObjectContext];
store.saveContextOnCommit = NO;
FEMDeserializer *deserializer = [[FEMDeserializer alloc] initWithStore:store];

NSArray *objects = [deserializer collectionFromRepresentation:dictionary mapping:mapping];

With the code above FEM will map, but won't save result to the database. i.e. [context save:&error]; won't get called.

@vishaldeshai
Copy link
Author

where to use mapping object in your code??

@dimazen
Copy link
Contributor

dimazen commented Sep 2, 2017

@vishaldeshai I've updated my comment, please check it out.

@vishaldeshai
Copy link
Author

saveContextOnCommit saves the object in coredata
how to prevent that?

@dimazen
Copy link
Contributor

dimazen commented Oct 27, 2017

@vishaldeshai hello.
You have to configure an underlying FEMManagedObjectStore by instantiating it manually and setting saveContextOnCommit to NO.

@vishaldeshai
Copy link
Author

Hello,
FEMMapping *mapping = [_PlannVisit defaultMapping];// your mapping here
FEMManagedObjectStore *store = [[FEMManagedObjectStore alloc] initWithContext:[_PlannVisit getContext]];
store.saveContextOnCommit = NO;
FEMDeserializer *deserializer = [[FEMDeserializer alloc] initWithStore:store];
if (ary){
NSArray *objects = [deserializer collectionFromRepresentation:ary mapping:mapping];
self.aVisitsFlwups = [[NSMutableArray alloc] initWithArray:objects];
}
Here is my code but it stores in db why??

@dimazen
Copy link
Contributor

dimazen commented Oct 27, 2017

How are you fetching those objects? Most likely that they're temporary. Try to close your app right after [deserializer collectionFromRepresentation:ary mapping:mapping]; to see that your objects was in memory.

@vishaldeshai
Copy link
Author

that objects are feting from server.

@dimazen
Copy link
Contributor

dimazen commented Oct 27, 2017

I meant how did you figure out that objects are in the database?

@vishaldeshai
Copy link
Author

For me, there is two scenario like i am store some objects in db and some objects are not mapping but not store.

once i ma fetching stored object then also got mapped object(i.e i mensioned above)

@dimazen
Copy link
Contributor

dimazen commented Oct 27, 2017

@vishaldeshai can you show the output of the debugger command:

po [[objects first] objectID]

Please, run it on a clean install.

@rayWille
Copy link

rayWille commented Oct 24, 2018

@vishaldeshai
If you want to create array of core data objects without storing in db and want to use in app then try to create core data entity with [NSManagedObjectContext MR_defaultContext] and don't perform save operation
i think it will help you

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

No branches or pull requests

3 participants