Skip to content

Instantly share code, notes, and snippets.

@OctoberHammer
Created January 15, 2017 14:17
Show Gist options
  • Save OctoberHammer/348ea3dd84ce4736814f060d69b75e37 to your computer and use it in GitHub Desktop.
Save OctoberHammer/348ea3dd84ce4736814f060d69b75e37 to your computer and use it in GitHub Desktop.
class ParseLayer {
var categoriesDelegate: CategoriesDelegate?
var categories: [String:PFObject] = [:]
//class
//let query = PFQuery(className: "Categories")
func getAllCategories() {
let query = PFQuery(className: "Categories")
query.fromLocalDatastore()
query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
if error == nil && !(objects!.count == 0) {//Массив не может быть пустым
// The find succeeded.
print("Successfully retrieved \(objects!.count) from Local DataStore")
// Do something with the found objects
if let objects = objects, var categoriesDelegate = self.categoriesDelegate {
for object in objects {
categoriesDelegate.categories[object.objectId ?? ""] = object
}
} else {
//Если ошибка или массив пустой - тогда премся на бэкэнд
self.getAllCategoriesFromBackend()
}
} else {
self.getAllCategoriesFromBackend()
// Log details of the failure
//print("Error: \(error!) \(error!.userInfo)")
}
}
}
func getAllCategoriesFromBackend() {
let query = PFQuery(className: "Categories")
query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) from Local DataStore")
// Do something with the found objects
if let objects = objects, var categoriesDelegate = self.categoriesDelegate {
for object in objects {
categoriesDelegate.categories[object.objectId ?? ""] = object
}
PFObject.pinAll(inBackground: objects)
} else {
}
} else {
// Log details of the failure
//print("Error: \(error!) \(error!.userInfo)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment