Advertisement
Larme

Untitled

Apr 29th, 2020
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.39 KB | None | 0 0
  1. //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
  2. extension Organization {
  3.     static func makeFromOrganization(_ organization: Organization1) -> [Organization] {
  4.         let returnValue = Organization()
  5.         returnValue.address = organization.address
  6.         returnValue.name = organization.name
  7.         ...
  8.         return returnValue
  9.     }
  10. }
  11.  
  12.  
  13.  
  14. func saveOrganizations(_ organizations: [Organization]) -> [Organization] {
  15.  
  16.     let fails: [Organization] = [] //We return the failed insert in case?
  17.     sharedInstance.database!.open()
  18.  
  19.     organizations.forEach( { anOrganisation in
  20.         let succeed = sharedInstance.database!.executeUpdate("INSERT INTO Organization (address,Name,...) VALUES ( ?, ?, ?, ?, ?,)",withArgumentsIn: [anOrganisation.name ?? "",anOrganisation.address ?? "",...])
  21.         if succeed == false {
  22.             fails.append(anOrganisation)
  23.         }
  24.     })
  25.    
  26.     sharedInstance.database!.close()
  27.     return fails
  28. }
  29.  
  30.  
  31. do {
  32.     let result = try JSONDecoder().decode(Root.self, from: dataOfAllOrganizations!)
  33.     let organizations = result.organizations.map{ Organization.makeFromOrganization($0) }
  34.     let failed = sharedInstance.saveOrganization_Lists(organizations)
  35.     //What to do if failed is no empty?
  36. } catch {
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement