第一次用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
- 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
- 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
- 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
- 33. F8 2015 - Running at Scale on Parse
https://www.youtube.com/watch?v=3wFQiQXQbto