布尔值在我可以分配之前更改值

时间:2018-04-18 05:52:35

标签: objective-c spotify

我有一个奇怪的问题,我的布尔变量在更改之前我可以将它分配给一个变量,以便在Spotify SDK的反应本机代码中读取。

我已经确定了确切位置的变化,我正在试图找出原因:

我有一个名为isLoggedIn的布尔值,如果成功登录,则设置为1。然后,此变量将loggedIn设置为一个布尔值,该值将发送到我的React Native代码:

else
{
    // login successful
    NSLog(@"What is the value of isLoggedIn %@", [self isLoggedIn]);
    ////THIS shows that isLoggedIn still equal to 1.
    [self initializePlayerIfNeeded:[RNSpotifyCompletion onComplete:^(id unused, RNSpotifyError* unusedError) {
        NSLog(@"What is the value of isLoggedIn right after %@", [self isLoggedIn]);
        /////NOW the isLoggedIn variable is now equal to 0.


        // do UI logic on main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [authController.presentingViewController dismissViewControllerAnimated:YES completion:^{
                _loggingIn = NO;
                NSLog(@"What is the value of isLoggedIn %@", [self isLoggedIn]);
                NSNumber* loggedIn = [self isLoggedIn]; /////this is the line we need to be == 1
                resolve(loggedIn);
                if(loggedIn.boolValue)
                {
                    [self sendEvent:@"login" args:@[]];
                }
            }];
        });
    }]];
}

所以显然我认为这是由于这条线路正确吗?:

[self initializePlayerIfNeeded:[RNSpotifyCompletion onComplete:^(id unused, RNSpotifyError* unusedError)

正在运行此方法:initializePlayerIfNeeded但是当我在此方法结束时读取isLoggedIn变量的值时,它仍然显示布尔值1(查看最后一行):

-(void)initializePlayerIfNeeded:(RNSpotifyCompletion*)completion
{

    if(![self hasPlayerScope])
    {
        [completion resolve:nil];
        return;
    }

    // ensure only one thread is invoking the initialization at a time
    BOOL initializedPlayer = NO;
    NSError* error = nil;
    BOOL allowCaching = (_cacheSize.unsignedIntegerValue > 0);
    @synchronized(_player)
    {
        // check if player is already initialized
        if(_player.initialized)
        {
            initializedPlayer = YES;
        }
        else
        {
            initializedPlayer = [_player startWithClientId:_auth.clientID audioController:nil allowCaching:allowCaching error:&error];
        }
    }

    // handle initialization failure
    if(!initializedPlayer)
    {
        [completion reject:[RNSpotifyError errorWithNSError:error]];
        return;
    }

    // setup player
    _player.delegate = self;
    _player.playbackDelegate = self;
    if(allowCaching)
    {
        _player.diskCache = [[SPTDiskCache alloc] initWithCapacity:_cacheSize.unsignedIntegerValue];
    }

    // attempt to log in the player
    [self loginPlayer:completion];

    NSLog(@"What is the value of isLoggedIn %@", [self isLoggedIn]);
    ////THIS shows that isLoggedIn still equal to 1.

}

因此该方法的值仍然等于1,但为什么在该方法返回后,下一行显示isLoggedIn等于0?我对objC非常新,所以每一点知识对我都有帮助。我正在使用来自github的这个React Native Spotify库:https://github.com/lufinkey/react-native-spotify

您可以在此处找到我正在使用的确切文件:https://github.com/lufinkey/react-native-spotify/blob/master/ios/RNSpotify.m

你还需要什么来帮助解决这个问题?

编辑:这是改变isLoggedIn

的方法
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isLoggedIn)
{

    if(!_initialized)
    {
        return @NO;
    }
    else if(_auth.session == nil)
    {
        return @NO;
    }
    return @YES;
}

0 个答案:

没有答案