FastEasyMapping icon indicating copy to clipboard operation
FastEasyMapping copied to clipboard

Mapping but not store

Open vishaldeshai opened this issue 7 years ago • 29 comments

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.

vishaldeshai avatar Aug 31 '17 08:08 vishaldeshai

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]]

dimazen avatar Sep 01 '17 10:09 dimazen

Suppose i have array of dictionary then??

vishaldeshai avatar Sep 01 '17 10:09 vishaldeshai

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

dimazen avatar Sep 01 '17 10:09 dimazen

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???

vishaldeshai avatar Sep 01 '17 10:09 vishaldeshai

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

dimazen avatar Sep 01 '17 10:09 dimazen

NSObject is super class for either nsaray or nsdictionary.

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

vishaldeshai avatar Sep 01 '17 10:09 vishaldeshai

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?

dimazen avatar Sep 01 '17 10:09 dimazen

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??

vishaldeshai avatar Sep 01 '17 10:09 vishaldeshai

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.

dimazen avatar Sep 01 '17 10:09 dimazen

Can you elaborate more??

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

vishaldeshai avatar Sep 01 '17 10:09 vishaldeshai

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.

dimazen avatar Sep 01 '17 11:09 dimazen

http://supersales.co:8080/salesprodev/getallleadsstatus?getleadsjson={CreatedBy:20,CompanyID:1}&TokenID=B12F3D170F46CCB00AE429741C770DB8&UserID=20

Plz hit above url and see my data.

vishaldeshai avatar Sep 01 '17 11:09 vishaldeshai

I have already created Coredata ManagedObject Subclass.

vishaldeshai avatar Sep 01 '17 11:09 vishaldeshai

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

dimazen avatar Sep 02 '17 12:09 dimazen

Yes

vishaldeshai avatar Sep 02 '17 12:09 vishaldeshai

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.

dimazen avatar Sep 02 '17 12:09 dimazen

nested context means how??

vishaldeshai avatar Sep 02 '17 12:09 vishaldeshai

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.

dimazen avatar Sep 02 '17 12:09 dimazen

where to use mapping object in your code??

vishaldeshai avatar Sep 02 '17 12:09 vishaldeshai

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

dimazen avatar Sep 02 '17 12:09 dimazen

saveContextOnCommit saves the object in coredata how to prevent that?

vishaldeshai avatar Oct 27 '17 09:10 vishaldeshai

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

dimazen avatar Oct 27 '17 09:10 dimazen

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??

vishaldeshai avatar Oct 27 '17 09:10 vishaldeshai

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.

dimazen avatar Oct 27 '17 10:10 dimazen

that objects are feting from server.

vishaldeshai avatar Oct 27 '17 10:10 vishaldeshai

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

dimazen avatar Oct 27 '17 10:10 dimazen

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)

vishaldeshai avatar Oct 27 '17 10:10 vishaldeshai

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

po [[objects first] objectID]

Please, run it on a clean install.

dimazen avatar Oct 27 '17 10:10 dimazen

@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

rayWille avatar Oct 24 '18 06:10 rayWille