Question

For some purpose I have created singleton that organize all works with dropbox via this new object.

So I have next singleton with initialization code as below:

- (id)init
{
    self = [super init];
    if (self)
    {
        self.cloudName = @"Dropbox";

        DBSession* dbSession = [[DBSession alloc] initWithAppKey:DP_App_KEY
                                                       appSecret:DP_App_SECRET
                                                            root:ROOT];

        [DBSession setSharedSession:dbSession];

        self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
        self.restClient.delegate = self;
    }
    return self;
}

If you can see I init DBSession directly in my singleton init method. The app call init method and seems everything should work good.

So when I logged in the app calls this in open url method:

if ([[DBSession sharedSession] isLinked])

and app output here that the dropbox is linked, but when I try to to get metadata for root @"/" folder (list of files) and call this method:

[self.restClient loadMetadata:folder];

the app output this error:

[WARNING] DropboxSDK: error making request to /1/metadata/dropbox - (403) Parameter not found: oauth_token
2014-04-25 00:30:15.652 PDF-Notes[75257:70b] Error loading metadata: Error Domain=dropbox.com Code=403 "The operation couldn’t be completed. (dropbox.com error 403.)" UserInfo=0xb666600 {path=/, error=Parameter not found: oauth_token}

But when I rerun app via Xcode one more time everything works good. Also if I set DBSession in app delegate it also works. I really did not understand why it does not work in singleton because the invocation methods works in the same way, but only if we init DBSession in app it works without problems.

Was it helpful?

Solution

First of all - you have to create REST client only after session became linked - this will ensure you that DBRestClient will be connected to real session.

Second - problems can be if there are a Dropbox application installed on device with your application. In this case your application will push up the authentification to DB application.

And third - your implementation of - (id) init for your class is not a singleton. Singleton is a pattern when your object lives only in one common object and no recreation can be done. I Objective C we have to look after retain counts as well (in no-Arc code shode be overridden memory management methods). But in anyway your init method (often called shared instance) should look like that:

static Singleton * sharedInstance= nil;
+(Singleton *) sharedInstance
{

    static dispatch_once_t once_token = 0;
    dispatch_once(&once_token, ^
                  {
                      sharedInstance =  [Singleton  new];
                  });
    return sharedInstance ;
} 

Hope this will help you with DB integration

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top