Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //That's the quick fix, but in fact, you should read about https://stackoverflow.com/questions/44450114/how-to-use-swift-4-codable-in-core-data and avoid Organization1 & Organization
- extension Organization {
- static func makeFromOrganization(_ organization: Organization1) -> [Organization] {
- let returnValue = Organization()
- returnValue.address = organization.address
- returnValue.name = organization.name
- ...
- return returnValue
- }
- }
- func saveOrganizations(_ organizations: [Organization]) -> [Organization] {
- let fails: [Organization] = [] //We return the failed insert in case?
- sharedInstance.database!.open()
- organizations.forEach( { anOrganisation in
- let succeed = sharedInstance.database!.executeUpdate("INSERT INTO Organization (address,Name,...) VALUES ( ?, ?, ?, ?, ?,)",withArgumentsIn: [anOrganisation.name ?? "",anOrganisation.address ?? "",...])
- if succeed == false {
- fails.append(anOrganisation)
- }
- })
- sharedInstance.database!.close()
- return fails
- }
- do {
- let result = try JSONDecoder().decode(Root.self, from: dataOfAllOrganizations!)
- let organizations = result.organizations.map{ Organization.makeFromOrganization($0) }
- let failed = sharedInstance.saveOrganization_Lists(organizations)
- //What to do if failed is no empty?
- } catch {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement