Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在异步请求完成之前返回self_Objective C_Image_Asynchronous_Request_Afnetworking - Fatal编程技术网

Objective c 在异步请求完成之前返回self

Objective c 在异步请求完成之前返回self,objective-c,image,asynchronous,request,afnetworking,Objective C,Image,Asynchronous,Request,Afnetworking,嘿,到目前为止我有这个代码 - (id)initWithDictionary:(NSDictionary *)aDictionary { self = [super init]; if (!self) { return nil; } _userAvatar = [self getUserAvatar:[aDictionary objectForKey:@"user_avatar"]]; _userId = [aDictionary ob

嘿,到目前为止我有这个代码


- (id)initWithDictionary:(NSDictionary *)aDictionary {
    self = [super init];
    if (!self) {
        return nil;
    }

    _userAvatar = [self getUserAvatar:[aDictionary objectForKey:@"user_avatar"]];
    _userId = [aDictionary objectForKey:@"user_id"];
    _username = [aDictionary objectForKey:@"username"];
    _userEmail = [aDictionary objectForKey:@"user_email"];
    return self;
}


- (UIImage *)getUserAvatar:(NSString *)avatarPath {

    __block UIImage *avatarImage = [[UIImage alloc]init];
    if ([avatarPath length] != 0) {
        [[AFFnBAPIClient sharedClient] setDefaultHeader:@"Accept" value:@"image/png"];
        [[AFFnBAPIClient sharedClient] getPath:FNB_DOWNLOAD_PATH parameters:[NSDictionary dictionaryWithObject:avatarPath forKey:@"avatar"] success:^(AFHTTPRequestOperation *operation, id image) {
            avatarImage = image;
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error");
        }];
    } else {
        UIImage *placeholderImage = [UIImage imageNamed:@"placeholder.png"];
        avatarImage = placeholderImage;
    }
    return avatarImage;
}
我的问题是在异步请求完成之前调用return self,如何在没有同步请求的情况下在请求完成的情况下返回self

- (id)initWithDictionary:(NSDictionary *)aDictionary {
    self = [super init];
    if (self) {

        // for starters, it's a placeholder
        self.userAvatar = [UIImage imageNamed:@"placeholder.png"];

        // now go get the avatar, fill it in whenever the get finishes.
        // notice there's no return value.
        [self getUserAvatar:[aDictionary objectForKey:@"user_avatar"]];

        // make sure these are retained (or strong in ARC) properties.
        // your code looked like it was releasing these
        self.userId = [aDictionary objectForKey:@"user_id"];
        self.username = [aDictionary objectForKey:@"username"];
        self.userEmail = [aDictionary objectForKey:@"user_email"];
    }
    return self;
}

// see - no return value.  it's asynch
- (void)getUserAvatar:(NSString *)avatarPath {

    // don't need a __block var here
    if ([avatarPath length] != 0) {
        [[AFFnBAPIClient sharedClient] setDefaultHeader:@"Accept" value:@"image/png"];
        [[AFFnBAPIClient sharedClient] getPath:FNB_DOWNLOAD_PATH parameters:[NSDictionary dictionaryWithObject:avatarPath forKey:@"avatar"] success:^(AFHTTPRequestOperation *operation, id image) {

            // self got a retain+1 while this was running.
            self.userAvatar = image;

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error");
        }];
    }
}

如果您不希望它在请求完成之前返回,这不是同步请求的定义吗?您的问题似乎是“如何在不阻塞的情况下进行阻塞?”或者您的意思是“如何将make AFFnBAPIClient转换为同步请求?”因此,我猜在这种情况下,如果没有同步请求,就不可能返回self?主要问题是我调用的是
User*User=[[User alloc]initWithDictionary:forUser];[userDic setObject:user forKey:@“user”]
因此userDic在用户对象获得必须发生的AvataRimage之前已经拥有该用户对象。阿凡达将在之后某个时候出现。我提供的代码将在图像到达时更新userAvatar。也许你在更新用户界面时遇到了问题?现在,在块self.userAvatar=image中的行上放置一个断点;检查您是否拥有良好的自我和良好的图像。图像已设置,但在我将userDic传递给另一个控制器后,因此我无法通过
[[userDic objectForKey@“user”]userAvatar]
访问真实图像,因为它已设置为占位符图像,我想这有点复杂;)。上面的整个代码都在一个类User.m中,它用我上面注释中的代码初始化,并传递给它的委托方法
[self.delegate loadUserData:userDic]@daiikota,对不起,没有代码很难理解。也许你可以勾选这个答案,然后用相关代码打开一个新问题?这里的方法将初始化该对象,为userAvatar设置占位符并返回它。稍后,它会将化身设置为获取的图像(覆盖占位符)。无论该对象去哪里,在其他词典中,或者在任何地方,它都应该有一个占位符或者在它到达后获取的图像。您的UI可能需要意识到映像已更改并自行更新,但这是一个不同的问题/问题。它最终需要向新控制器添加一个观察者并观察映像:)