Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private func preloadData() {
- let preloadedDataKey = "didPreloadData"
- let userDefaults = UserDefaults.standard
- if userDefaults.bool(forKey: preloadedDataKey) == false {
- guard let urlPath = Bundle.main.url(forResource: "Services", withExtension: "plist", subdirectory: "Data") else {
- return
- }
- print(urlPath)
- let backgroundContext = persistentContainer.newBackgroundContext()
- persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
- backgroundContext.perform {
- if let arrayContents = NSArray(contentsOf: urlPath) as? [Service] {
- do {
- for item in arrayContents {
- let serviceObject = Service(context: backgroundContext)
- serviceObject.name = item.name!
- serviceObject.slug = item.slug!
- serviceObject.url = item.url!
- }
- try backgroundContext.save()
- userDefaults.set(true, forKey: preloadedDataKey)
- } catch {
- print(error.localizedDescription)
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement