简体   繁体   中英

IOS 7 AFNetworking 2.0 view refresh issue

first I'd like to say that the issue I am having is in regards to how my own code is set, not IOS or AFNetworking.

I am going to show 1 example where a view is not refreshing immediately even though the updated JSON response object from the server is being recieved.

There are 2 views being used in the first view the button below takes the user to a second view where he can upload an image that will then be displayed in the first view.

- (IBAction)editImage:(id)sender {
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
TDUserProfileViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"TDUserProfileImageEditViewController"];
[self.navigationController pushViewController:vc animated:YES];
}

Below is the code that uploads the new data to the server and then gets new data regarding the images's address on the server.

- (IBAction)dataSubmitToServer{


//    NSLog(@"photo: %@", self.photoData);
 NSString * userID = [[NSUserDefaults standardUserDefaults] objectForKey:USERID];
//make the call to the web API
NSString* command = @"setProfilePhoto";
self.params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command", userID, @"userid", nil];

//////////////////
NSLog(@"%@", self.params);


self.photoName = @"newname.png";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];



manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:BaseURLString parameters:self.params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileData:UIImageJPEGRepresentation(self.userPhoto.image, 1)
                                name:@"image"
                            fileName:self.photoName
                            mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {


    NSLog(@"JSON: %@", responseObject);

       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", [error localizedDescription]);

    [TDPublicFunctions showDefaultAlert:@"Error" body:[error description]];
}];

[self getNewProfileData];


[[NSUserDefaults standardUserDefaults] synchronize];


}

-(void)getNewProfileData{
/*start getting new data for profile pic and status message*/

NSString* command = @"displayProfileData";

NSString * userID = [[NSUserDefaults standardUserDefaults]  objectForKey:USERID];

NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command",  userID,@"userid", nil];

NSLog( @"%@", params);
[SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:BaseURLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    [SVProgressHUD dismiss];
    if([responseObject objectForKey:@"error"])
    {
        [[[UIAlertView alloc] initWithTitle:@"Data Retrieval Error" message:[responseObject description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
    }
    else
    {
        NSString * profilePhoto = [[[responseObject objectForKey:@"result"] objectAtIndex:0] objectForKey:@"profile_picture"];
        NSString * status = [[[responseObject objectForKey:@"result"] objectAtIndex:0] objectForKey:@"status"];
        NSString * statusMsg = [[[responseObject objectForKey:@"result"] objectAtIndex:0] objectForKey:@"status_message"];

        if(profilePhoto && ![profilePhoto isKindOfClass:[NSNull class]])
            [[NSUserDefaults standardUserDefaults] setObject:profilePhoto forKey:PROFILE_PHOTO];
        if(status && ![status isKindOfClass:[NSNull class]])
            [[NSUserDefaults standardUserDefaults] setObject:status forKey:STATUS];
        if(statusMsg && ![statusMsg isKindOfClass:[NSNull class]])
            [[NSUserDefaults standardUserDefaults] setObject:statusMsg forKey:STATUS_MESSAGE];

    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
//end of getting data for status message and profile pic
}

Now once this above process is over the HUD is supposed to stop and when the back button on the view is hit there should be a newly uploaded image in the first view. The code below is the last of many things that I have tried in order to get the view with the image to refresh: -(void) viewWillAppear:(BOOL)animated {

NSString * profPic = [[NSUserDefaults standardUserDefaults] objectForKey:PROFILE_PHOTO];
NSLog(@"profilepic: %@", profPic);
//    NSString* status = [[NSUserDefaults standardUserDefaults] objectForKey:STATUS];
NSString * statusMsg = [[NSUserDefaults standardUserDefaults] objectForKey:STATUS_MESSAGE];
if(profPic)
    [photo loadIconForProduct:profPic];

if(statusMsg)
    statusMsgView.text = statusMsg;

//self.view=nil;
//[self viewDidLoad];
[self getFriendsList];
[photo setNeedsDisplay];
[self.view setNeedsDisplay];
}

After an image is uploaded it may work the first time an image is uploaded but it does not work if the user were to press the editImage button a second time and upload another image.

What is it that is being done wrong? Let me know if there is anything else that I can supply.

Thanks.

As I can see here, you are getting a URL back as the result of the profilePhoto. So you might as well use the AFNetworking function:

Im presuming photo is a UIImageView.

[photo setImageWithURL:[NSURL URLWithString:profPic] placeholderImage:[UIImage imageNamed:@"somePlaceholderImage"]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM