SlideShare a Scribd company logo
Parse
ymow
BaaS, backend as a service
BaaS is a model for providing web and mobile app developers
with a way to link their applications to backend cloud storage
and APIs exposed by back end applications while also
providing features such as user management, push
notifications, and integration with social networking services.
by wikipedia
Why parse.com
• clearly document
• simply operate
• synchronization automatic
• push notification server
• local data store
• open source
• Scale
第一次用Parse就深入淺出
第一次用Parse就深入淺出
第一次用Parse就深入淺出
第一次用Parse就深入淺出
第一次用Parse就深入淺出
• Download & unzip the SDK
• Add dependencies

dependencies {

compile 'com.parse.bolts:bolts-android:1.+'

compile fileTree(dir: 'libs', include: 'Parse-*.jar')}

• manifest

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
• Application#onCreate()

// Enable Local Datastore.

Parse.enableLocalDatastore(this);

Parse.initialize(this, "key", "key");
Quick Start
Basic Operate
• Saving Objects
• Retrieving Objects and Query data
• Updating Objects
• Arrays
• Deleting Objects
• ParseObject contains key-value pairs of JSON-compatible
data. This data is schemaless, which means that you don't
need to specify ahead of time what keys exist on each
ParseObject. You simply set whatever key-value pairs you
want, and our backend will store it.
ParseObject
ParseObject parseTest = new ParseObject("test");
parseTest("ClickNumber", “1”);
parseTest("cheatMode", “Anoymorus”);
String objectId = parseTest.getObjectId();
parseTest.saveInBackground();
saveParseObject
Retrieving Objects
ParseQuery<ParseObject> query =
ParseQuery.getQuery("test");
query.getInBackground("xWMyZ4YEGZ", new
GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
} else {
// something went wrong
}
}
});
ObjectID
ParseQuery<ParseObject> query =
ParseQuery.getQuery(“test");
query.whereEqualTo(“ClickNumber”,”1”);
query.getInBackground("xWMyZ4YEGZ", new
GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
} else {
// something went wrong
}
}
}); Query Objects
ParseQuery<ParseObject> query =
ParseQuery.getQuery("test");
query.getInBackground("xWMyZ4YEGZ", new
GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
object.put(“CkickNumber”,”2”);
object.saveInBackground();
} else {
// something went wrong
}
}
});
Updating Objects
JSONObject userProfile = new JSONObject();


ParseObject Androidhacker = new ParseObject("test");

Androidhacker.put("profile", userProfile);
Androidhacker.saveInBackground();
JSONObject
Array type
• To delete an object from the Parse Cloud

myObject.deleteInBackground();
• delete a single field from an object

// After this, the playerName field will be empty

myObject.remove("playerName");

// Saves the field deletion to the Parse Cloud

myObject.saveInBackground();

Delete Object
Member
• Sign up / Sign in
• Current User
• ParseFile(Image, File …etc)
• Anonymous Users
• Facebook Users
ParseUser.logInInBackground("RickisRich", "password", new
LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
user.setUsername(“RickisGOD”)
user.saveInBackground();
} else {
// Signup failed. Look at the ParseException to see what
happened.
}
}
});
Sign in
ParseUser user = new ParseUser();
user.setUsername("my name");
user.setPassword("my pass");
user.setEmail("email@example.com");
// other fields can be set just like with ParseObject
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
Sign up
ParseUser.getCurrentUser().setUsername(“RickisGod”);
ParseUser.getCurrentUser().saveInBackground();
ParseUser
ParseUser can be get or set in anywhere of the project, but
when you set anything, remember to saveInBackground, or
it will not be save.
ParseUser.getCurrentUser().getObjectId();

ParseUser.getCurrentUser().getEmail();
ParseFile FacebookProfilePicture = new
ParseFile("image.png", facebookByte.toByteArray());


ParseUser.getCurrentUser().put("image",
FacebookProfilePicture);

ParseUser.getCurrentUser().saveInBackground();
ParseFile
Getting started with ParseFile is easy. First, you'll need to
have the data in byte[] form and then create a ParseFile
with it. In this example, we'll just use a string:
ParseAnonymousUtils.logIn(new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
if (e != null) {
Log.d("MyApp", "Anonymous login failed.");
} else {
Log.d("MyApp", "Anonymous user logged in.");
}
}
});
An anonymous user is a user that can be created without a
username and password but still has all of the same
capabilities as any other ParseUser. After logging out, an
anonymous user is abandoned, and its data is no longer
accessible. No matter have Internet connection or not,
ParseAnonymous will never be null value.
ParseAnonymous
• Add ParseFacebookUtilsV4.jar to your project
• Application#onCreate()

ParseFacebookUtils.initialize(context);
• OnActivityResult

@Override

protected void onActivityResult(int requestCode, int
resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

ParseFacebookUtils.onActivityResult(requestCode,
resultCode, data);
Parse Facebook
ParseFacebookUtils.logInWithReadPermissionsInBackground(
this, permissions, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Facebook
login.");
} else if (user.isNew()) {
makeOpenGraphRequest();
Log.d("MyApp", "User signed up and logged in through
Facebook!");
} else {
makeOpenGraphRequest();
Log.d("MyApp", "User logged in through Facebook!");
}
}
}); Parse Facebook
• Application#onCreate() 

Parse.enableLocalDatastore();
• Parse Query

ParseQuery<ParseObject> query =
ParseQuery.getQuery(“test");
query.whereEqualTo(“ClickNumber”,”1”);
query.getInBackground("xWMyZ4YEGZ", new
GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
…
});
Local Data store
• saveInBackground() => saveEventually()
Online and offline
第一次用Parse就深入淺出
Write good queries, the Golden Rule:
Minimize search space
第一次用Parse就深入淺出
第一次用Parse就深入淺出
第一次用Parse就深入淺出
F8 2015 - Running at Scale on Parse
https://www.youtube.com/watch?v=3wFQiQXQbto

More Related Content

第一次用Parse就深入淺出

  • 2. BaaS, backend as a service BaaS is a model for providing web and mobile app developers with a way to link their applications to backend cloud storage and APIs exposed by back end applications while also providing features such as user management, push notifications, and integration with social networking services. by wikipedia
  • 3. Why parse.com • clearly document • simply operate • synchronization automatic • push notification server • local data store • open source • Scale
  • 9. • Download & unzip the SDK • Add dependencies
 dependencies {
 compile 'com.parse.bolts:bolts-android:1.+'
 compile fileTree(dir: 'libs', include: 'Parse-*.jar')}
 • manifest
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> • Application#onCreate()
 // Enable Local Datastore.
 Parse.enableLocalDatastore(this);
 Parse.initialize(this, "key", "key"); Quick Start
  • 10. Basic Operate • Saving Objects • Retrieving Objects and Query data • Updating Objects • Arrays • Deleting Objects
  • 11. • ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each ParseObject. You simply set whatever key-value pairs you want, and our backend will store it. ParseObject
  • 12. ParseObject parseTest = new ParseObject("test"); parseTest("ClickNumber", “1”); parseTest("cheatMode", “Anoymorus”); String objectId = parseTest.getObjectId(); parseTest.saveInBackground(); saveParseObject
  • 13. Retrieving Objects ParseQuery<ParseObject> query = ParseQuery.getQuery("test"); query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (e == null) { // object will be your game score } else { // something went wrong } } }); ObjectID
  • 14. ParseQuery<ParseObject> query = ParseQuery.getQuery(“test"); query.whereEqualTo(“ClickNumber”,”1”); query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (e == null) { // object will be your game score } else { // something went wrong } } }); Query Objects
  • 15. ParseQuery<ParseObject> query = ParseQuery.getQuery("test"); query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (e == null) { // object will be your game score object.put(“CkickNumber”,”2”); object.saveInBackground(); } else { // something went wrong } } }); Updating Objects
  • 16. JSONObject userProfile = new JSONObject(); 
 ParseObject Androidhacker = new ParseObject("test");
 Androidhacker.put("profile", userProfile); Androidhacker.saveInBackground(); JSONObject Array type
  • 17. • To delete an object from the Parse Cloud
 myObject.deleteInBackground(); • delete a single field from an object
 // After this, the playerName field will be empty
 myObject.remove("playerName");
 // Saves the field deletion to the Parse Cloud
 myObject.saveInBackground();
 Delete Object
  • 18. Member • Sign up / Sign in • Current User • ParseFile(Image, File …etc) • Anonymous Users • Facebook Users
  • 19. ParseUser.logInInBackground("RickisRich", "password", new LogInCallback() { public void done(ParseUser user, ParseException e) { if (user != null) { // Hooray! The user is logged in. user.setUsername(“RickisGOD”) user.saveInBackground(); } else { // Signup failed. Look at the ParseException to see what happened. } } }); Sign in
  • 20. ParseUser user = new ParseUser(); user.setUsername("my name"); user.setPassword("my pass"); user.setEmail("email@example.com"); // other fields can be set just like with ParseObject user.put("phone", "650-253-0000"); user.signUpInBackground(new SignUpCallback() { public void done(ParseException e) { if (e == null) { // Hooray! Let them use the app now. } else { // Sign up didn't succeed. Look at the ParseException // to figure out what went wrong } } }); Sign up
  • 21. ParseUser.getCurrentUser().setUsername(“RickisGod”); ParseUser.getCurrentUser().saveInBackground(); ParseUser ParseUser can be get or set in anywhere of the project, but when you set anything, remember to saveInBackground, or it will not be save. ParseUser.getCurrentUser().getObjectId();
 ParseUser.getCurrentUser().getEmail();
  • 22. ParseFile FacebookProfilePicture = new ParseFile("image.png", facebookByte.toByteArray()); 
 ParseUser.getCurrentUser().put("image", FacebookProfilePicture);
 ParseUser.getCurrentUser().saveInBackground(); ParseFile Getting started with ParseFile is easy. First, you'll need to have the data in byte[] form and then create a ParseFile with it. In this example, we'll just use a string:
  • 23. ParseAnonymousUtils.logIn(new LogInCallback() { @Override public void done(ParseUser user, ParseException e) { if (e != null) { Log.d("MyApp", "Anonymous login failed."); } else { Log.d("MyApp", "Anonymous user logged in."); } } }); An anonymous user is a user that can be created without a username and password but still has all of the same capabilities as any other ParseUser. After logging out, an anonymous user is abandoned, and its data is no longer accessible. No matter have Internet connection or not, ParseAnonymous will never be null value. ParseAnonymous
  • 24. • Add ParseFacebookUtilsV4.jar to your project • Application#onCreate()
 ParseFacebookUtils.initialize(context); • OnActivityResult
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 ParseFacebookUtils.onActivityResult(requestCode, resultCode, data); Parse Facebook
  • 25. ParseFacebookUtils.logInWithReadPermissionsInBackground( this, permissions, new LogInCallback() { @Override public void done(ParseUser user, ParseException err) { if (user == null) { Log.d("MyApp", "Uh oh. The user cancelled the Facebook login."); } else if (user.isNew()) { makeOpenGraphRequest(); Log.d("MyApp", "User signed up and logged in through Facebook!"); } else { makeOpenGraphRequest(); Log.d("MyApp", "User logged in through Facebook!"); } } }); Parse Facebook
  • 26. • Application#onCreate() 
 Parse.enableLocalDatastore(); • Parse Query
 ParseQuery<ParseObject> query = ParseQuery.getQuery(“test"); query.whereEqualTo(“ClickNumber”,”1”); query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (e == null) { … }); Local Data store
  • 27. • saveInBackground() => saveEventually() Online and offline
  • 29. Write good queries, the Golden Rule: Minimize search space
  • 33. F8 2015 - Running at Scale on Parse https://www.youtube.com/watch?v=3wFQiQXQbto