Wiki

Clone wiki

ParseFacade / Home

Welcome

I recently discovered http://parse.com/ and started to use it for my mobile developments. Up until this point, I've been using Google AppEngine, but parse.com is better.

  • you can still get started for free
  • the documentation is great and contains plenty for examples
  • the API are generally well designed

Example

// save
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();

// retrieve
ParseQuery query = new ParseQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
  public void done(ParseObject gameScore, ParseException e) {
    if (e == null) {
      // do something with gameScore
    } else {
      // something went wrong
    }
  }
});

// read application values
int score = gameScore.getInt("score");
String playerName = gameScore.getString("playerName");
boolean cheatMode = gameScore.getBoolean("cheatMode");

// read special system managed values
String objectId = gameScore.getObjectId();
Date updatedAt = gameScore.getUpdatedAt();
Date createdAt = gameScore.getCreatedAt();

What's wrong

  • The treatment between application and system values is not fair.
  • There's too many strings constants all over the place.
  • I can't use auto-completion on my application values.

An Alternative

  • allow auto-completion of field names
  • compiler will check types for you
  • define names and corresponding type in one place (DRY)
  • uses filter/sort by example
  • augment, but doesn't hide native API, so not functionality is lost
  • doesn't generate any extra gets and puts to native API
  • doesn't interfere with partial updates
// save
GameScore gameScore = GameScore.PF.create();
gameScore.score(1337);
gameScore.playerName("Sean Plott");
gameScore.cheatMode(true);
gameScore.parseObject().saveInBackground();

// retrieve
GameScore.PF.query().parseQuery().getInBackground("xWMyZ4YEGZ", new GetCallback() {
	@Override
	public void done(ParseObject gameScore, ParseException e) {
		if (e == null) {
			// do something with gameScore
			GameScore gameScore2 = GameScore.PF.wrap(gameScore);
		} else {
			// something went wrong
		}
	}
});

// read application values
int score = gameScore.score();
String playerName = gameScore.playerName();
boolean cheatMode = gameScore.cheatMode();

// read special system managed values
String objectId = gameScore.parseObject().getObjectId();
Date updatedAt = gameScore.parseObject().getUpdatedAt();
Date createdAt = gameScore.parseObject().getCreatedAt();

Query and Sorting

//
Query<GameScore> query = GameScore.PF.query();
query.equalTo().playerName("Sean Plott");
query.orderAsc().score();
query.findInBackground(new ListCallback<GameScore>() {
	@Override
	public void error(ParseException e) {
		// something went wrong
	}

	@Override
	public void done(List<GameScore> list) {
		// do something with gameScore
		if (list.size() > 0) {
			GameScore gameScore2 = list.get(1);
		}
	}
});

Finally

import shared.parse.ParseBase;
import shared.parse.ParseFacade;

public interface GameScore extends ParseBase {
	public String playerName();
	public void playerName(String in);

	public Integer score();
	public void score(int in);

	public Boolean cheatMode();
	public void cheatMode(boolean in);
	
	public static final ParseFacade<GameScore> PF = ParseFacade.get(GameScore.class);
}
  • define get and set like methods, but without the prefix
  • return type should be objects and not primitive type for most flexibility
  • interface name is use as ParseObject className
  • method names are used as the field names for get, put, sorting and filtering
  • the method "parseObject" is reserved

Others

When do you switch from Basic to Pro?

Basic vs Pro

Apache License Version 2.0

http://www.apache.org/licenses/

Default

Updated