Skip to content

Instantly share code, notes, and snippets.

@desamtralized
Created June 3, 2015 03:12
Show Gist options
  • Save desamtralized/bccc4c9ba22e7c6fef49 to your computer and use it in GitHub Desktop.
Save desamtralized/bccc4c9ba22e7c6fef49 to your computer and use it in GitHub Desktop.
Accessing ParseObject properties in a easy way using Kotlin Data Class and Delegate.
//Define your sublclass of ParseObject
ParseClassName("Person")
public data class Person : ParseObject() {
var name: String by ParseDelegate<String>()
var age: Int by ParseDelegate<Int>()
var avatar: ParseFile by ParseDelegate<ParseFile>()
}
//Define ParseDelegate
public class ParseDelegate<T> {
fun get(parseObj: ParseObject, propertyMetadata: PropertyMetadata): T {
return parseObj.get(propertyMetadata.name) as T
}
fun set(parseObj: ParseObject, propertyMetadata: PropertyMetadata, a: Any) {
parseObj.put(propertyMetadata.name, a)
}
}
//Use it
//fecth some Person
person.age //will return an Int
person.avatar //will return a ParseFile
person.age = 30 //will set the new value in the ParseObject using put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment