couchbaselabs/GrocerySync-Android

Error while creating a new document "Trying to put wrong _id to this: com.couchbase.lite.Document"

Keshava11 opened this issue · 8 comments

Hi I am creating a new project whose build.gradle file is below.
Since I am fetching the couchbase dependencies from maven-artifacts so not including submodules.
Do we need to add those too ?

But get error when I try to add a new document
Trying to put wrong _id to this: com.couchbase.lite.Document

apply plugin: 'com.android.application'

android { 
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "testproj.com.testprojetc"
        minSdkVersion 9
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }

    // workaround for "duplicate files during packaging of APK" issue
    // see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.0.0'

    // By default, this depends on couchbase lite via the maven artifact
    // Using this as I am using Android Studio version 0.8.9 from Stable channel
    compile 'com.couchbase.lite:couchbase-lite-android:1.0.0'
}

Since I am fetching the couchbase dependencies from maven-artifacts so not including submodules.
Do we need to add those too ?

Nope. You can use either the maven artifacts (default behavior) or use the submodules (requires change).

If you needed to modify the core couchbase lite code to fix a bug, you'd want to switch over to using the submodules. (or if you needed to use a version that was newer than the maven artifacts)

But get error when I try to add a new document. Trying to put wrong _id to this: com.couchbase.lite.Document

Can you paste the code you are using to create the document, as well as the full error stacktrace?

Also, have you modified GrocerySync or are you using the exact version in github?

Actually I am trying to recreate the Grocery-Sync project at my end and add some more features.

Note : I am using Android Studio 0.8.9 from Stable channel. Will it make any difference in usage of the library . Should I get you the complete code ?

private Document createAndAddNotes(String iContent) throws CouchbaseLiteException {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        UUID uuid = UUID.randomUUID();
        Calendar calendar = GregorianCalendar.getInstance();
        long currentTime = calendar.getTimeInMillis();
        String currentTimeString = dateFormatter.format(calendar.getTime());

        String id = currentTime + "-" + uuid.toString();

        Document document = database.createDocument();

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("_id", id);
        properties.put("text", iContent);
        properties.put("check", Boolean.FALSE);
        properties.put("created_at", currentTimeString);
        document.putProperties(properties);

        return document;
    }

Log

10-17 11:53:56.800    6937-6937/testproj.com.testprojetc W/CBLite﹕ Trying to put wrong _id to this: com.couchbase.lite.Document@413728a8 properties: {check=false, _id=1413527036806-69db7e6a-05e6-4841-8fea-2a2c0c78c25f, created_at=2014-10-17T11:53:56.806Z, text=hello Wilt}
10-17 11:54:33.750    6937-6937/testproj.com.testprojetc E/copybit﹕ Error opening frame buffer errno=13 (Permission denied)
10-17 11:54:33.750    6937-6937/testproj.com.testprojetc W/Adreno200-EGLSUB﹕ <updater_create_surface_state:342>: updater_create_surface_state failed to open copybit, error: -13
10-17 11:54:33.750    6937-6937/testproj.com.testprojetc D/libc﹕ pt_debug : pthread_create->start_routine=0x517cb149, tls=0x53f57f00, arg=0x5191af60
10-17 11:54:33.750    6937-7194/testproj.com.testprojetc D/libc﹕ pt_debug : __thread_entry->func=0x517cb149
    , tls=0x53f57f00, arg=0x5191af60
10-17 11:54:33.750    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Mapped buffer base:0x51841000 size:24576 offset:0 fd:66
10-17 11:54:33.750    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Mapped buffer base:0x40019000 size:4096 offset:0 fd:69
10-17 11:54:33.760    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Mapped buffer base:0x51847000 size:24576 offset:0 fd:75
10-17 11:54:33.760    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Mapped buffer base:0x5163d000 size:4096 offset:0 fd:77
10-17 11:54:34.320    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Unmapping buffer  base:0x51841000 size:24576
10-17 11:54:34.320    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Unmapping buffer  base:0x40019000 size:4096
10-17 11:54:34.320    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Unmapping buffer  base:0x51847000 size:24576
10-17 11:54:34.320    6937-6937/testproj.com.testprojetc D/memalloc﹕ ion: Unmapping buffer  base:0x5163d000 size:4096
10-17 11:54:35.710    6937-6937/testproj.com.testprojetc W/CBLite﹕ Trying to put wrong _id to this: com.couchbase.lite.Document@41403ab0 properties: {check=false, _id=1413527075719-7bf02b7a-9b28-4656-a3ef-7fea2e0f7f37, created_at=2014-10-17T11:54:35.719Z, text=hello Wiltghh}

Sample code that I am running is here
https://github.com/Keshava11/NotesSync

I even tried using the couchbase lite library version 1.0.3 that works fine with the Android Studio 0.8.9 but didn't worked out. Still get the same error.

Note : I am using Android Studio 0.8.9 from Stable channel. Will it make any difference in usage of the library

That should work, but please let me know if you run into any issues.

@WilliamHoang which version of Android Studio are you on? 0.8.9?

10-17 11:53:56.800 6937-6937/testproj.com.testprojetc W/CBLite﹕ Trying to put wrong _id to this: com.couchbase.lite.Document@413728a8 properties: {check=false, _id=1413527036806-69db7e6a-05e6-4841-8fea-2a2c0c78c25f, created_at=2014-10-17T11:53:56.806Z, text=hello Wilt}

Thanks for your code snippet and full error, now it's clear what the problem is.

When you call database.createDocument(), it will create the _id for you. In this case, it created 1413527036806-69db7e6a-05e6-4841-8fea-2a2c0c78c25f as the _id. When you try to save a revision with a different _id value (currentTime + "-" + uuid.toString(); in your case), then it throws an error.

You have two ways to fix it:

  • Continue using database.createDocument(), but do not pass in an _id field in your properties HashMap, and just use the one generated by couchbase lite
  • Call getDocument(your_id) and pass in your custom ID.

In this case, I'd recommend the first approach.

Which features are you planning to add -- would you be willing to contribute them back into GrocerySync?

Also have you seen TodoLite? It has more features than GrocerySync.

Relevant API docs:

http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/database/database/index.html#document-createdocument
http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/database/database/index.html#document-getdocumentstring-id

@tleyden : I am on the latest: v0.8.9

@Keshava11:
Are the errors resolved on your ends? Chiming in on @tleyden, which features are you looking on adding?

Hi @tleyden @WilliamHoang.
I was away last weekend. I will be checking it today itself.

I thought of adding a reminder field to it which will prompt user when clock strikes.