-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Issue with NSManagedObject class conforming to Mappable #68
Comments
Hi, I have not actually used ObjectMapper with an NSManagedObject as of yet. However perhaps @brandonroth can provide insight, as he is the one who contributed the above function. |
YES! I'm having the same problem as well. |
The required init is necessary so that ObjectMapper can instantiate objects during the mapping process. I can't think of a way to remove it at the moment. I'll put some more thought into it. |
The latest release has an update to the Mappable protocol. It no longer requires the default initializer function. This may resolve the issue you all have been having. |
Great stuff! |
It is still required in 0.9. Am I missing something? |
|
Since it still has to be declared, I get the compiler error:
And when I add |
Hi @bonebox , I'm facing the same problem. How do you resolved it ? |
Hi @vincent-ch , I actually didn't resolve it. I ended up going with a different library, called Sync. It is awesome if you're mapping into managed objects! |
Thanks for the hint. If anyone would be interested in this case, I don't know if this is the proper way to do it, but I resolved it this way:
|
Hey, I use the method of @vincent-ch to map my JSON and store the POJO to the database, I succeed with one class, but how do that with a member which is an Array of another object ? |
Hi @Wii2zO , I faced the same problem and solved it that way: I created a temp array (this will not be persisted) to let ObjectMapper do its magic and then initialized the NSSet with that array using Hope this helps! (: Here's a full exemple:
|
Thank you for your response ! |
Hi there, I've been trying @vincent-ch 's code but I'm having some troubles mapping custom NSManaged classes. Here's an example of my class : import Foundation class Credential: NSManagedObject, Mappable {
} I have a relationship to a Member NSManaged class, and to-many relationships to Host and ExtensionFields. My goal is to build JSON data from the credential class (including the relationships). I did have some success with a custom parser ( see my post on stack-overflow : http://stackoverflow.com/questions/31672558/ios-swift-serialize-a-nsmanagedobject-to-json-with-relationships/31697576?noredirect=1#comment51347010_31697576) BUT to conform to the json data that the web service needs, I need to map my class because the variables don't have the same names etc. That said, I'm getting error (found nil while unwrapping) with @vincent-ch' s code (the difference being I use Set and not NSSet, could it be the problem here ?) Is there a way to map my JSON string just to change variables name or can ObjectMapper do the magic for me ? Thanks for your help, Hugo |
Hi @HRiffiod , I don't know if the difference between Set and NSSet could be the problem, but I always try to use only the types generated by XCode (from the .xcdatamodel file). Could you try with NSSet? And could you gave us the code line where you get the error? |
Hi @vincent-ch , so I added this code to my class : // temporary array tos allow mapping
and I'm getting the error on this line : credentialHosts = Set(credentialHostsArray!) Which is not that surprising because my array is declared as optional and is never initialized/filled before this line is called. so my JSON string should contain something like "credentialHosts" : [](just an empty array as long as I don't add Hosts in my app). Maybe I'm misunderstanding some initialization part (I'm kinda new to Swift). Should I add some code to my Host class for example ? At this time it looks like this : import Foundation class Host: NSManagedObject {
} PS : I'd like to make sure that it does not come from an misunderstanding instead of NSSet because I'm trying to use as much as possible native types in my app... |
I think you're almost there. I would change your function that way (I guessed the WS parameters' names, maybe you have to change them):
ObjectMapper will do the mapping of HostsArray and ExtensionFieldsArray for you. The (almost) only thing my code does is to save this mapping in a temporary array (i.e. credentialHostsArray) and then "convert it" into a NSSet/Set (i.e. credentialHosts). |
Ok, I got it! Yes, my solution works only if the parameters are always set (mandatory fields). In your case your last answer sounds good, you could also check if the arrays are nil after the mapping and then set an empty array in that case. Let me know if your solution worked! |
So I tried creating empty arrays, if prevents from crashing, first step :) However, event when hard-coding some value to a host, my Json output does not contain my Host array, any idea why it does not work ? Here is the code : // create a host Array to avoid crashing if nil
and here is the json output : { |
My solution works for a mapping from JSON and you're working to JSON. But nevertheless, I don't understand why your hosts are empty, because you're filling the array used in the ObjectMapper's mapping process. If I understand the ObjectMapper's doc right, the Did you try to debug your code and see what happens during the mapping process? |
Guys, what do you if you have multiple ManagedObjectContext? I don't see any options how to tell mapper which context is should create entites in.. |
@vincent-ch, you might be the only person on Earth to get this working. I followed your example (thx!) yet my code crashes calling mapping(map) with EXC_BAD_ACCESS(code=1). Seen anything like this? |
Hi guys, I'm sorry, I don't work on that mobile project anymore, so I'll do my best to help you without XCode. @f1nality : I set the context with the following line @marcospolanco : the |
@bonebox how do you convert objects to JSON when using Sync? Are you still using ObjectMapper for it? Can't get them both to work together UPDATE: nevermind, I think I figured it out 3lvis/Sync#165 |
Yep, that's it. |
What is the proper place to perform the function managedObjectContext.save() ? Or it is unnecessary? |
@f1nality I have this working with multi contexts, one way to achieve this is to use a custom mapping context object. The custom mapping context object can hold an NSManagedObjectContext in which you want to save the object. To extract this NSManagedObjectContext from the init function we can do something like map.context.myManagedObjectContext. This still works cleanly with @vincent-ch solution. |
@vincent-ch Hi I am working on a project that uses objectMapper for its use. Please kindly explain what exactly is being done by this 2 functions:
|
@GABHISEKBUNTY look into the |
@tristanhimmelman Actually I just wanted to know what exactly is going on these 2 methods. A small quick explannation will be quite helpful. And what is the need of these functions? |
Ah then I will leave that to @vincent-ch as I am not familiar with his implementation |
@tristanhimmelman But what exactly this implementation helps for? |
@GABHISEKBUNTY Hi, I'm sorry, I don't work on that mobile project anymore. I cannot remember how I come to this solution. And sorry for the late answer, I was on holidays. The first one overrides the default constructor to call the super constructor including the default context (I get it with DBUtils().getManagedObjectContext()). The second one get the entity "Gallery" from the default context (always by calling DBUtils().getManagedObjectContext()), call the super constructor and then call the mapping method. I hope it will help, just ask if you need some more precise informations. |
Hi @vincent-ch , is this code still working on Swift3 on iOS 10 ? cause i cant use DButils, it says "Use of unresolved identifier 'DBUtils'" is DBUtils come from another library ? |
@calvinsug Sorry, I know this part was a little bit confusing because the DBUtils was part of my project. If I remember well, this method simply returns the default context. |
@vincent-ch i used your code and its working perfectly. But when i relaunch the app all the save datas in core data db get disappeared. I used the same code
Any help will be appreciated. |
Hi @niks1990 , |
@niks1990 could you solve your problem? I am facing the same! After relaunch I losing the data. |
Calling mapping(map) in required_init resulted in duplicates. Here's what is working for me: import CoreData @objc(Room) override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { required public init?(map: Map) {
} public func mapping(map: Map) {
} |
So many years for this answer... but for me the mapping(map) crash happened when: 1 - the class prototype was missing @objc(class) declaration; |
Hi,
I'm trying to map directly JSON to a class model inheriting NSManagedObject. I created an extension that conform to Mappable and implement mapping.
At compile time, I got a Swift compiler error, usr/bin/swiftc failed to exit code 1.
It might be due to the init() method of the Mappable protocol. When reading the Mapper.swift class I found the following:
which would be the function to use in order to set properties of the entity once created.
Could you post an example of an NSManagedObject class conforming to Mappable?
Thanks
The text was updated successfully, but these errors were encountered: