Native couchbase lite IOS view

89 views
Skip to first unread message

nadia echi

unread,
Apr 2, 2014, 4:58:13 AM4/2/14
to mobile-c...@googlegroups.com
Hi,

How can i check the existence of my views when the app is launched , if it's not the will be created ,
like :

 //********** customer View search:

    

    CBLView* view = [self.database viewNamed: @"client/docView"];

    [view setMapBlock: ^(NSDictionary* doc, void (^emit)(id key, id value)) {

        NSString* type = [doc objectForKey: @"type"];


        NSArray * value = [NSArray arrayWithObjects:

                         [doc objectForKey: @"id"],

                         [doc objectForKey: @"description1"],

                         nil];

        if ([type isEqualToString:@"CLIE"]) emit([doc objectForKey:@"_id"], value);

  } version: @"1.0"];

    

    //*********** product varianty


   view = [self.database viewNamed: @"prodVersion/docView"];

    [view setMapBlock: ^(NSDictionary* doc, void (^emit)(id key, id value)) {

        NSString* type = [doc objectForKey: @"type"];

        

        NSArray * value1 = [NSArray arrayWithObjects:

                           [doc objectForKey: @"code"],

                           [doc objectForKey: @"id"],

                           [doc objectForKey: @"versionCode"],

                           nil];

        if ([type isEqualToString:@"product"]) emit([doc objectForKey:@"_id"], value1);

    } version: @"1.0"];


because when i insert new doc and i do get to the view, the result is not updating, i have to reopen the app to have the view updated !!

Thanks
Nadia ECHI

Jens Alfke

unread,
Apr 2, 2014, 10:06:36 AM4/2/14
to mobile-c...@googlegroups.com

On Apr 2, 2014, at 1:58 AM, nadia echi <echi...@gmail.com> wrote:

How can i check the existence of my views when the app is launched , if it's not the will be created ,

A view is created the first time you access it. The view and its index are persistent, but you have to register the map and reduce blocks on every launch. You should do that right after opening the database, definitely before querying the view.

because when i insert new doc and i do get to the view, the result is not updating, i have to reopen the app to have the view updated !!

How are you querying the view?

—Jens

nadia echi

unread,
Apr 3, 2014, 3:47:38 AM4/3/14
to mobile-c...@googlegroups.com
i m querying the view from service with angularjs like this :

 GETProductsForSearch: function(callback) {

                           return   $http({method: "GET", url: database+"/_design/prodVersion/_view/docView", cache: $templateCache}).success(callback).error(function(data, status) {alert("failed to get product view = "+status); });

                           },



How can i register the map and reduce blocks on every launch ??

Jens Alfke

unread,
Apr 4, 2014, 11:07:50 AM4/4/14
to mobile-c...@googlegroups.com


On Thursday, April 3, 2014 12:47:38 AM UTC-7, nadia echi wrote:
i m querying the view from service with angularjs like this :
Oh, this is a PhoneGap app? So you're using a native map function just for performance?
 

How can i register the map and reduce blocks on every launch ??

Well, you already are, aren't you? That's the Objective-C code you posted in the first message. They have to be registered for the view to work at all. Make sure they're getting registered right after the CBLDatabase is opened.

--Jens

nadia echi

unread,
Apr 4, 2014, 11:26:50 AM4/4/14
to mobile-c...@googlegroups.com

Oh, this is a PhoneGap app? So you're using a native map function just for performance?
 yes it is : a PhoneGap application and native view for performance 

How can i register the map and reduce blocks on every launch ??

Well, you already are, aren't you? That's the Objective-C code you posted in the first message. They have to be registered for the view to work at all. Make sure they're getting registered right after the CBLDatabase is opened.


I registered the views after opening the CBLDatabase .
But why i still have the same problem when i insert new doc and i quered the view from JS, the result is not updating. So i have to reopen the app to have the view updated !!
 

Jens Alfke

unread,
Apr 4, 2014, 12:37:18 PM4/4/14
to mobile-c...@googlegroups.com
On Apr 4, 2014, at 8:26 AM, nadia echi <echi...@gmail.com> wrote:

I registered the views after opening the CBLDatabase .

Can you show the code that opens the database and registers the views?

But why i still have the same problem when i insert new doc and i quered the view from JS, the result is not updating. So i have to reopen the app to have the view updated !!

So the sequence is like this?

(1) insert new document
(2) wait for HTTP response to the PUT
(3) Query the view
(4) Query result doesn’t include the new document
(5) Press Home button
(6) Reopen app
(7) Query the view
(8) Query result includes new document

In step 6 are you making sure the app process is killed, or are you just reactivating the already-running app?

—Jens

nadia echi

unread,
Apr 4, 2014, 12:45:11 PM4/4/14
to mobile-c...@googlegroups.com


Le vendredi 4 avril 2014 18:37:18 UTC+2, Jens Alfke a écrit :
On Apr 4, 2014, at 8:26 AM, nadia echi <echi...@gmail.com> wrote:


Can you show the code that opens the database and registers the views?


 self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

    

    CBLManager *manager = [CBLManager sharedInstance];

    //[CBLView setCompiler: [[[CBLJSViewCompiler alloc] init] autorelease]];

    // create a database

    NSError *error;

    self.database = [manager createDatabaseNamed: @"catafoto" error: &error];

    if (!self.database)

        NSLog(@"Couldn't open database (error=%@)" ,error);

    NSURL *url =manager.internalURL;

   

    //********** customer View search:

    

    CBLView* view = [self.database viewNamed: @"client/docView"];

    [view setMapBlock: ^(NSDictionary* doc, void (^emit)(id key, id value)) {

        NSString* type = [doc objectForKey: @"type"];


        NSArray * value = [NSArray arrayWithObjects:

                         [doc objectForKey: @"id"],

                         [doc objectForKey: @"description1"],

                         nil];

        if ([type isEqualToString:@"CLIE"]) emit([doc objectForKey:@"_id"], value);

  } version: @"1.0"];

    

    //*********** product varianty


   view = [self.database viewNamed: @"prodVersion/docView"];

    [view setMapBlock: ^(NSDictionary* doc, void (^emit)(id key, id value)) {

        NSString* type = [doc objectForKey: @"type"];

        

        NSArray * value1 = [NSArray arrayWithObjects:

                           [doc objectForKey: @"code"],

                           [doc objectForKey: @"id"],

                           [doc objectForKey: @"versionCode"],

                           nil];

        if ([type isEqualToString:@"product"]) emit([doc objectForKey:@"_id"], value1);

    } version: @"1.0"];

 

......ect




(6) Reopen app ----> mean i killed the process and relaunch it : with this i can found my list updated




Jens Alfke

unread,
Apr 4, 2014, 12:56:44 PM4/4/14
to mobile-c...@googlegroups.com, Chris Anderson
The setup code looks good, so the views should be updating correctly. Are you certain that this code runs before your JavaScript GUI starts up?

I can’t help you with the JavaScript part because I don’t know the JS APIs for Couchbase Lite. Chris Anderson would probably be the best person; he’s on this list and I’ll cc him. (Chris, the whole thread is here.)

—Jens

J. Chris Anderson

unread,
Apr 15, 2014, 5:39:21 PM4/15/14
to mobile-c...@googlegroups.com, Chris Anderson
I don't see a reason why this shouldn't be working. Maybe angular is caching something it shouldn't? There should be something short of relaunching the process that you can do to fire off another view query. Perhaps a manual XHR request from the safari developer console would let you see what's going on more clearly?
Reply all
Reply to author
Forward
0 new messages