Skip to content

Commit

Permalink
handleGetTips now writes the results to DB using ContentProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanZoR committed Feb 28, 2015
1 parent a8bb91a commit 7139aba
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app/src/main/java/com/tipz/app/control/services/TipzService.java
@@ -1,8 +1,9 @@
package com.tipz.app.control.services;

import android.app.IntentService;
import android.content.Intent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.commonsware.cwac.wakeful.WakefulIntentService;
Expand All @@ -14,11 +15,11 @@
import com.tipz.app.model.entities.TipEntity;
import com.tipz.app.model.rest.TipzApi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.converter.GsonConverter;

/**
Expand Down Expand Up @@ -90,5 +91,23 @@ protected void doWakefulWork(Intent intent) {
*/
private void handleGetTips() {
List<TipEntity> tipEntities = mTipzApi.listTips();

ContentValues[] allContent = new ContentValues[tipEntities.size()];

// Create content values for each entity
int contentIndex = 0;
for (TipEntity tip : tipEntities) {
ContentValues content = new ContentValues();
content.put(TipEntity.DB.CREATED_TIMESTAMP, tip.createdTimestamp);
content.put(TipEntity.DB.TITLE, tip.title);

// Add the content to the all of contents
allContent[contentIndex] = content;
contentIndex++;
}

// Store the entities in a database
int inserted = getContentResolver().bulkInsert(TipEntity.CONTENT_URI, allContent);

This comment has been minimized.

Copy link
@SeanZoR

SeanZoR Mar 4, 2015

Author Owner

Tip: Bulk/Transactional inserts are tremendously faster. When inserting multiple values into the DB directly, or when doing that through a ContentProvider, make sure to do it in a transactional manner.

Log.d(TAG, String.format("Inserted %d tips", inserted));
}
}
}

0 comments on commit 7139aba

Please sign in to comment.