简体   繁体   中英

How can i check if data is successfully stored in coredata, next time when i launch an app ? - iOS Swift

i have successfully fetched data through api and stored them in coredata. Now next time when user launches an app i want to check if there is local data available then i want to load local data, not server data.

You should apply two things for fulfilling your scenario.

1) First Fetch Data from CoreData.

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
        request.returnsObjectsAsFaults = false
        do {
            let result = try context.fetch(request)
            for data in result as! [NSManagedObject] {
               print(data.value(forKey: "username") as! String)
          }

        } catch {

            print("Failed")
        }

if result is available then Don't need to call API.

2)If Fetched Result is nil Call the API and Save Data Into CoreData

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