-
-
Save andrea-prearo/c5bfc26cb927f0e7ad8c46e1cd7bda7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserController: UserControllerProtocol { | |
[...] | |
private let persistentContainer: NSPersistentContainer | |
[...] | |
init(persistentContainer: NSPersistentContainer) { | |
self.persistentContainer = persistentContainer | |
} | |
[...] | |
} | |
private extension UserController { | |
func parse(_ jsonData: Data) -> Bool { | |
do { | |
guard let codingUserInfoKeyManagedObjectContext = CodingUserInfoKey.managedObjectContext else { | |
fatalError("Failed to retrieve context") | |
} | |
// Clear storage and save managed object instances | |
if currentPage == 0 { | |
clearStorage() | |
} | |
// Parse JSON data | |
let managedObjectContext = persistentContainer.viewContext | |
let decoder = JSONDecoder() | |
decoder.userInfo[codingUserInfoKeyManagedObjectContext] = managedObjectContext | |
_ = try decoder.decode([User].self, from: jsonData) | |
try managedObjectContext.save() | |
return true | |
} catch let error { | |
print(error) | |
return false | |
} | |
} | |
[...] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment