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

Model.mapping(map: Map(mappingType: .fromJSON, JSON: meetingJson)) not saving ID #1391

Open
addame2 opened this issue Jun 30, 2022 · 0 comments

Comments

@addame2
Copy link

addame2 commented Jun 30, 2022

params- mapping before != nil Optional(3134620)
params- mapping after Optional(3134620) nil
params- saveInContext(meetingJson: Optional(3134620)
params- meetingFind 0 why my id is nil ? any idea?

    public static func saveInContext(meetingJson: [String : Any], source: MeetingSource, completion: ((MeetingModel?) -> Void)? = nil) {
        
        MagicalRecord.save { context in
            context.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.overwriteMergePolicyType)
            let ctx = MapManagedObjectContext(context: context)
            var meeting = MeetingModel.mr_findFirst(with: NSPredicate(format: "id=%@", meetingJson["id"] as! CVarArg), in: ctx.ctx)
            if meeting != nil {
                print("params- mapping before != nil", meeting?.id )
                meeting?.mapping(map: Map(mappingType: .fromJSON, JSON: meetingJson))
                print("params- mapping after", meeting?.id, meetingRes?.id )
            } else {
                print("params- mapping before == nil", meeting?.id )
                meeting = Mapper<MeetingModel>(context: ctx).map(JSON: meetingJson)
                print("params- mapping before == nil", meeting?.id)
            }
            meeting?.source = Int16(source.rawValue)
            
            if let profJson = meetingJson["prof"] as? [String : Any] {
                if let login = profJson["login"] as? String {
                    let profile = OtherUserModel.mr_findFirst(with: NSPredicate(format: "login = %@", login), in: ctx.ctx)
                    if profile != nil {
                        profile!.mapping(map: Map(mappingType: .fromJSON, JSON: profJson))
                        meeting?.prof = profile
                    } else {
                        let profile = Mapper<OtherUserModel>(context: ctx).map(JSON: profJson)
                        meeting?.prof = profile
                    }
                }
            }
            print("params- saveInContext(meetingJson:", meeting?.id )
            
            if let meetingFind = MeetingModel.mr_findFirst(with: NSPredicate(format: "source == \(MeetingSource.my.rawValue)")) {
                print("params- meetingFind", meetingFind.id )
            }
            
        } completion: { result, error in
            completion?(getMyMeeting())
        }
        
    }


    public static func getMyMeeting() -> MeetingModel? {
        if let meeting = MeetingModel.mr_findFirst(with: NSPredicate(format: "source == \(MeetingSource.my.rawValue)")) {
            return meeting
        }
        return nil
    }
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

1 participant