Skip to content

Instantly share code, notes, and snippets.

@MariuszWisniewski
Last active May 10, 2016 23:46
Show Gist options
  • Save MariuszWisniewski/90653dba9b8382909e1a to your computer and use it in GitHub Desktop.
Save MariuszWisniewski/90653dba9b8382909e1a to your computer and use it in GitHub Desktop.
class Book : SCDataObject {
var title = ""
var subtitle = ""
// add SCFile property as any other property on your class
var cover : SCFile? = nil
}
class MyClass {
func sendImage() {
let book = Book()
if let image = UIImage(named: "MyImage.png") {
let imageData = UIImagePNGRepresentation(image)
book.cover = SCFile(aData: imageData)
book.saveWithCompletionBlock({ error in
guard error == nil else {
//handle error
print(error)
return
}
//saved ok
})
}
}
func getImage() {
let book = Book()
book.objectId = 3
book.fetchWithCompletion { error in
// here, after downloading the object from Syncano, it will only hold a reference to the file
// it's still up to developer to know best moment when to fetch the file
// if `storeDataAfterFetch` is set to `false`, file will be available
// only in `data` variable inside `fetchInBackgroundWithCompletion`
// if `storeDataAfterFetch` is set to `true`, file data will be also saved as book.data
// accesible later
book.cover?.storeDataAfterFetch = true
book.cover?.fetchInBackgroundWithCompletion({ data, error in
guard error == nil, let data = data else {
print(error)
return
}
//do something with your data
//e.g. make an UIImage out of it
let image = UIImage(data: data)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment