Created
January 15, 2017 14:17
-
-
Save OctoberHammer/348ea3dd84ce4736814f060d69b75e37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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