0

I am building an app, in which I want to populate UITableViewCell from CoreData. In CoreData I am able to save data from server and also able to fetch data. But when my internet is offline I am unable to retrive data from CoreData or there is no data in CoreData. How can I fetch last saved data from CoreData when my internet is offline?

I am saving data using following function:

func SetPremiumValues(Array : NSArray){
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext
    let entity =  NSEntityDescription.entityForName("PremiumJob", inManagedObjectContext:managedContext)
    for i in Array{
        let PremiumJob = NSManagedObject(entity: entity!,insertIntoManagedObjectContext: managedContext)
        if let rowData :NSDictionary = i as? NSDictionary{
            PremiumJob.setValue(rowData["company"], forKey: "company")
            PremiumJob.setValue(rowData["city"], forKey: "city")
            PremiumJob.setValue(rowData["id"], forKey: "id")
            let ImageName = rowData["user_logo"] as? String
            let image = "http://dev.jobsmarket.pk/uploads/user-logo/"+ImageName!
            let url = NSURL(string: image)
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                if let data = NSData(contentsOfURL: url!) {
                    PremiumJob.setValue(data, forKey: "user_logo")
                }
            }
            PremiumJob.setValue(rowData["title"], forKey: "title")
            PremiumJob.setValue(rowData["fe_short_description"], forKey: "fe_short_description")
        }
        do {
            try managedContext.save()
            print("saving premium job is this \(PremiumJob)")
            self.Premium.append(PremiumJob)
        } catch let error as NSError {
            print("Could not save \(error), \(error.userInfo)")
        }
    }
}

// fetching data using fetch request

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "PremiumJob")
    do{
        let results =  try managedContext.executeFetchRequest(fetchRequest)
        for ManagedObject in results {
            let managedObjectData : NSManagedObject = ManagedObject as! NSManagedObject
            print("premium data from fetch request \(managedObjectData)")
        }
    }
    catch{   
    }   
}
  • Find your sqlite file and check does it have required records or not. – Avt Aug 17 '16 at 12:16
  • where i can find it? i have looked in app folders but could't find anything – Zaid Tayyab Aug 17 '16 at 13:53
  • i am saving with this function, but when i re launch app all saved data removes. why is this happening? how can i fetch last saved data on relaunching the app? – Zaid Tayyab Aug 17 '16 at 14:21
  • Quite a strange question for "Senior iOS Developer" )) Sqlite file should be in an app's Documents folder. Check this http://stackoverflow.com/a/24291087/3140927 to find where is an appp's Documnets folder in iPhone Simulator – Avt Aug 17 '16 at 15:25
  • If the problem is that you can't **fetch** data when the internet is not available, then the code you want to look at is where you're trying to fetch the data and what happens there if there's no network connection. – Tom Harrington Aug 17 '16 at 16:15
  • Yes @avt, surprisingly i am a student not a "Senior iOS developer". I promise, one day i ll b one. thankx for your help :) – Zaid Tayyab Aug 18 '16 at 07:08
  • @TomHarrington i have updated code, please take a look. – Zaid Tayyab Aug 18 '16 at 07:11
  • Hi @ZaidTayyab, since when we need internet connection when fetching data from CoreData? – Vladislav Kovalyov Aug 18 '16 at 07:54
  • @VladislavKovalyov yes obviously, but there are apps which shows last streamed data, right? i wanted to do the same. on my view i am retrieving data from server and saving it to core data. what i want is, when internet is not available i want to populate my tableViewCell with last saved data. and when internet is available i ll retrieve updated data from server to core data. – Zaid Tayyab Aug 18 '16 at 08:07
  • @VladislavKovalyov we dont need internet, my case is that i am saving data on main thread just after the response from server using dataTaskWithRequest. when i retrieve it after relaunch of application i get nothing with fetch request. i get data from fetch request when it saves data to core data. not getting the last saved data. – Zaid Tayyab Aug 18 '16 at 08:12

0 Answers0