简体   繁体   中英

How Do I append CoreData Object with Swift?

I'd like to add myplaceworks from server to coredata Here is my coredata object

private var user: [User]?
private var myPlaceOfWorks: [MyPlaceOfWorks]?
private var context = sharedManagedObjectContext()

and I tried this workplace of parameter is already made class.

this class controls array in json

func addInfo(workplace: [Sub_JsonModel_MyPlaceOfWorks], rtnvalue: String) ->
(User?, MyPlaceOfWorks?) {

    guard let newUser: User = 
NSEntityDescription.insertNewObjectForEntityForName("User",  
inManagedObjectContext: self.context) as? User else{
        return (nil, nil)

    }

    guard let newWorkPlace: MyPlaceOfWorks = 
NSEntityDescription.insertNewObjectForEntityForName("MyPlaceOfWorks", 
inManagedObjectContext: self.context) as? MyPlaceOfWorks else {
        return (nil, nil)
    }

    newUser.rtnvalue = rtnvalue

    for one in workplace {

        newWorkPlace.activeyn = one.ActiveYN
        newWorkPlace.basic = one.Basic
        newWorkPlace.beacon = one.Beacon
        newWorkPlace.cpiseq = one.CPISEQ
        newWorkPlace.cpmpoweq = one.CPMPOWSEQ
        newWorkPlace.companyname = one.CompanyName
        newWorkPlace.etime = one.ETime
        newWorkPlace.gps_latitude = one.GPS_latitude
        newWorkPlace.gps_longitude = one.GPS_longitude
        newWorkPlace.placename = one.PlaceName
        newWorkPlace.stime = one.STime
        newWorkPlace.wifi = one.Wifi
        self.myPlaceOfWorks?.append(newWorkPlace)
        print("newWorkPlace \(newWorkPlace)")
        print("myPlaceOfWorks \(myPlaceOfWorks)")
    }

    self.saveContext { 
        //action
        print("newUser:: \(newUser)")

    }
    return (newUser, newWorkPlace)
}

private func saveContext(completion: (() -> Void)?) {
    do {
        try self.context.save()
        if let handler = completion {
            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                handler()
            })
        }
    } catch let error {
        print("can not save context \(error)")
    }
}

I can get rtnvalue but, I can't get myPlaceOfWorks array object! It is just nil How can I try this?

You've created only one object of MyPlaceOfWorks class and then rewriting properties of this object in the loop. Instead you need to create new instance on each loop iteration.

for one in workplace {
    if let newWorkPlace: MyPlaceOfWorks = NSEntityDescription.insertNewObjectForEntityForName("MyPlaceOfWorks", inManagedObjectContext: self.context) as? MyPlaceOfWorks {
        newWorkPlace.activeyn = one.ActiveYN
        newWorkPlace.basic = one.Basic
        newWorkPlace.beacon = one.Beacon
        newWorkPlace.cpiseq = one.CPISEQ
        newWorkPlace.cpmpoweq = one.CPMPOWSEQ
        newWorkPlace.companyname = one.CompanyName
        newWorkPlace.etime = one.ETime
        newWorkPlace.gps_latitude = one.GPS_latitude
        newWorkPlace.gps_longitude = one.GPS_longitude
        newWorkPlace.placename = one.PlaceName
        newWorkPlace.stime = one.STime
        newWorkPlace.wifi = one.Wifi
        self.myPlaceOfWorks?.append(newWorkPlace)
    }
}
self.saveContext { 
    //action
    print("newUser:: \(newUser)")

}

And as zcui93 has mentioned in comments you need to instantiate your myPlaceOfWorks variable:

private var myPlaceOfWorks = [MyPlaceOfWorks]()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM