3lvis / DATAStack

100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer
Other
214 stars 44 forks source link

How to retreive Data using Datastack #73

Closed jassimkm closed 8 years ago

jassimkm commented 8 years ago

Hi I am new to swift i have stored my json into coredata using sync . But i don't know how can I retrieve datas using DATAStack . Kindly help

3lvis commented 8 years ago

Hi @jassimkm!

The first rule of Core Data is to "never pass objects around threads" so if you're going to use this fetched data in your UI, use the dataStack.mainContext. If you're using this data for processing it and then saving it back to Core Data use a dataStack.performInNewBackgroundContext block.

Fetching data in DATAStack is the same as fetching data in Core Data, checkout any tutorials online on how to do this, you'll find tons. In short you'll do it like this:

let fetchRequest = NSFetchRequest(entityName: "Person")
do {
  let result = try context.executeFetchRequest(fetchRequest)
  // do something with result
} catch let error as NSError {
  print("Could not fetch \(error), \(error.userInfo)")
}
jassimkm commented 8 years ago

Thank You