koulmomo / android-instagram

Starter Project for Android One Week Bootcamp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Android Bootcamp] Project 1 - Day 4 - Instagram - ready for review

koulmomo opened this issue · comments

My app is complete, please review. /cc @codepathreview @codepath

👍 Nice work Mo! You've completed all the required parts of the assignment. All of tomorrow stories are optional. It's a great opportunity to circle back and tackle any of the optional stories earlier in the week or choose from one of the Friday optionals.

Below you can find the best practices for today's stories. Please read through this point-by-point to determine how you might be able to improve your submission.

  • What operations should occur in the Service
  • We definitely want to make the network request in the service.
  • While we're already in the service context, which runs on a background thread, you should do the following in the service as well:
    - parse the network response when it comes back
    - load the parsed objects into the DB
    - return the parsed objects to any listeners
  • Default value for a Service's exported attribute
  • When declared in the AndroidManifest.xml file, a Service is by default not accessible by other applications (i.e., exported = false) if it has no intent filters. See Service elements in Manifest File for a detailed explanation.
  • Avoid nested ViewGroups. Flatter view hierarchies are more performant
  • If possible, try to keep the view hierarchy as flat as possible. Nesting ViewGroups should be avoided, since this can affect the time that it takes to render the layout. See the following article on how Android draws views. A good number of ViewGroups do multiple traversals of their view hierarchies to calculate how much screen real estate all of its contents should receive.
  • When clearing rows in the DB, foreign key constraints must be taken into account
  • Because posts reference user and image rows, you cannot delete a user if the DB contains a post that references it.
  • Emptying the database needs to be done in this order:
  // this is the join table for posts and comments
  db.delete(TABLE_POSTS_COMMENTS, null, null);

  // posts reference both users and images
  db.delete(TABLE_POSTS, null, null);

  db.delete(TABLE_COMMENTS, null, null);

  // users and images refer to no other tables
  db.delete(TABLE_IMAGES, null, null);
  db.delete(TABLE_USERS, null, null);
  • Wrap transaction operations in a try / finally block
  • To ensure that db.endTransaction() is called regardless of whether an operation fails or not:
   db.beginTransaction();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
  • Convert InstagramPost class to Parcelable for easy array serialization
  • Because the InstagramPost class implements Serializable, to package a list of posts into an intent, a serializable wrapper is needed. This is fine for the assignment.
  • Parcelable: A cleaner alternative
    - The problem with InstagramPost is that it contains nested objects. See this article, which shows how nested parcelable objects are marshalled and demarshalled.
    - Once InstagramPost implements Parcelable instead of Serializable, you can do this:
  List<InstagramPost> posts = new ArrayList<InstagramPost>();

  Intent intent = new Intent();
  intent.putExtra("posts", posts);

  // Extracting data on receipt of intent
  List<InstagramPost> fetchedPosts = getIntent().getParcelableArrayListExtra("posts");