gpt4 book ai didi

ios - Objective-C:执行完方法后获得单例类方法的结果吗?

转载 作者:行者123 更新时间:2023-12-01 18:45:27 24 4
gpt4 key购买 nike

我有一个单例 class ,用于检查应用程序的登录状态。

单例类中有一个名为attemptToLogin的方法,该方法发出带有参数的http请求,并返回有关登录状态的json数据。 (对或错)

现在在主要TabBarViewController中,我做了以下工作:

@interface CustomTabBarController ()

@end

@implementation CustomTabBarController{
LoginCheckSingleton *loginSingleton;
dispatch_queue_t myCustomQueue;
}
-(void)viewDidAppear:(BOOL)animated{
myCustomQueue = dispatch_queue_create("com.myDomain.appName", NULL);
loginSingleton = [LoginCheckSingleton sharedInstance];
dispatch_sync(myCustomQueue, ^{
[loginSingleton attemptToLogin];
});
if([loginSingleton.loginstat isEqual: @"true"]){
NSLog(@"Logged In");
}else{
NSLog(@"Not Logged In");
[self performSegueWithIdentifier:@"goToLoginView" sender:self];
}
}
dispatch_sync在这里无法正常工作,我想在dispatch_sync内部执行该函数并获取结果,然后再执行其下的 if语句。但是 if语句是在dispatch_sync内部的块完成之前执行的。

这是 Singleton Class:
#import "LoginCheckSingleton.h"
#import "AFNetworking.h"
#import "SSKeychain.h"
#import "CustomTabBarController.h"
static LoginCheckSingleton *sharedInstance = nil;
@interface LoginCheckSingleton (){
NSString *serverURLString;
NSURL *serverURL;
NSString *userId;
NSString *password;
NSString *email;
NSMutableArray *jsonContents;
NSMutableDictionary *dictionary;
NSString *loggedInStatus;
bool loggedInTF;

}

@end
@implementation LoginCheckSingleton{
}


+ (LoginCheckSingleton*) sharedInstance {
static dispatch_once_t _singletonPredicate;
static LoginCheckSingleton *_singleton = nil;

dispatch_once(&_singletonPredicate, ^{
_singleton = [[super allocWithZone:nil] init];
});

return _singleton;
}

+ (id) allocWithZone:(NSZone *)zone {
return [self sharedInstance];
}

-(void)attemptToLogin{
// Retrieve credentials from Keychain
userId = [SSKeychain passwordForService:@"com.lazemni.iFresh"
account:@"ifreshUserId"];
password = [SSKeychain passwordForService:@"com.lazemni.iFresh"
account:@"ifreshPassword"];
email = [SSKeychain passwordForService:@"com.lazemni.iFresh"
account:@"ifreshEmail"];
if(email == nil || password == nil){

NSLog(@"empty username or password");
}else{
NSLog(@"not empty username or password");
serverURLString = @"http://www.lazemni.com/demo/ifresh/api/login/";
serverURLString = [serverURLString stringByAppendingString:email];
serverURLString = [serverURLString stringByAppendingString:@"/"];
serverURLString = [serverURLString stringByAppendingString:password];
NSLog(@"%@",serverURLString);
serverURL = [NSURL URLWithString:serverURLString];

NSURLRequest *request = [NSURLRequest requestWithURL:serverURL];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
jsonContents = [responseObject objectForKey:@"login"];
NSLog(@"%@",jsonContents);
dictionary = [jsonContents objectAtIndex:0];
loggedInStatus = [dictionary objectForKey:@"status"];
if([loggedInStatus isEqual: @"true"]){
NSLog(@"Succefully loggedin!");
[SSKeychain setPassword:[dictionary objectForKey:@"user_id"] forService:@"com.lazemni.iFresh" account:@"ifreshUserId"];
[SSKeychain setPassword:[dictionary objectForKey:@"email"] forService:@"com.lazemni.iFresh" account:@"ifreshEmail"];
[SSKeychain setPassword:[dictionary objectForKey:@"password"] forService:@"com.lazemni.iFresh" account:@"ifreshPassword"];
self.loginstat = @"true";
loggedInTF = true;
}else if([loggedInStatus isEqual: @"false"]){
NSLog(@"Wrong email/password combination!");
self.loginstat = @"false";
loggedInTF = false;
}
} failure:nil];
[operation start];
}
}


@end

我从来不了解dispatch_sync到底如何工作,
知道如何等待 [loginSingleton attemptToLogin];完成吗?

最佳答案

- (void)attemptToLogin:(void(^)(BOOL login))complete {
...
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
....
// After you get login status
complete(your_login_status_here);
}
}

并在 CustomTabBarController
-(void)viewDidAppear:(BOOL)animated{
myCustomQueue = dispatch_queue_create("com.myDomain.appName", NULL);
loginSingleton = [LoginCheckSingleton sharedInstance];

[loginSingleton attemptToLogin:^(loginStatus){
if([loginStatus isEqual: @"true"]){
NSLog(@"Logged In");
}else{
NSLog(@"Not Logged In");
dispatch_async(dispatch_get_main_queue(), {
[self performSegueWithIdentifier:@"goToLoginView" sender:self];
});
}

}];

}

关于ios - Objective-C:执行完方法后获得单例类方法的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com