触发通知后NSString不会更新

时间:2017-01-08 21:03:38

标签: ios objective-c

我的用户可以在他们的个人资料(ViewController)上传和/或拍照。也就是说,一旦照片被保存,我希望在保存后立即使用最新照片更新imageView。为此,我需要刷新NSString(secondLink)。理论上,我认为以下代码可以工作 - 例如成功保存时触发方法userpicUpdated(从服务器“重新获取”信息的方法)。也就是说,出于某种原因,下面的userpicUpdated方法只是在field_photo_path中提取旧数据,而不是更新数据。我怎样才能解决这个问题?

AppDelegate.m

#import "DIOSSession.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        [DIOSSession setupDios];

    }

ViewController.m

 - (void)userpicUpdated:(NSNotification *)note {                    

    }



     - (void)viewDidLoad {
        [super viewDidLoad];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(userpicUpdated:)
                                                     name:@"userpicUpdated" object:nil];

    }

    - (IBAction)savePhotoButton:(id)sender {
          [DIOSUser
         userUpdate:userData
         success:^(AFHTTPRequestOperation *op, id response) { /* Handle successful operation here */


             NSLog(@"User data updated!");
             self.activityInd.hidden = YES;
             [self.activityInd stopAnimating];


             [DIOSUser
              userGet:userData
              success:^(AFHTTPRequestOperation *op, id response) { /* Handle successful operation here */



                [[NSNotificationCenter defaultCenter] postNotificationName:@"userpicUpdated" object:nil];

                  NSDictionary *thisUser = userData;
                  NSLog(@"USER DATA GOTTEN %@", thisUser);

                  NSString *imageUpdate = thisUser[@"field_photo_path"][@"und"][0][@"value"];
                  NSLog(@"IMAGE STRING %@", imageUpdate);

                 NSURL *imageUrl = [NSURL URLWithString:imageUpdate];
                  [self.imageView setImageWithURL:imageUrl];
                  NSLog(@"IMAGE URL %@", imageUrl);




              }
              failure:^(AFHTTPRequestOperation *op, NSError *err) { /* Handle operation failire here */

                  NSLog(@"User data failed to update!");
              }
              ];




         }
         failure:^(AFHTTPRequestOperation *op, NSError *err) { /* Handle operation failire here */

          NSLog(@"User data failed to update!");
         }
         ];

        }

1 个答案:

答案 0 :(得分:1)

Looking at the source code on GithubDIOSUser userUpdate:没有重新加载用户信息,因此您需要在更新成功时拨打DIOSUser userGet:。成功userGet:后,您可以保存返回的用户数据,并通过点击UIImage更新NSNotificationCenter

成功时更新UIImage,您不需要使用NSNotificationCenter。是否值得检查imageView是否仍可访问(iOS是否已从内存中释放?)以避免崩溃。

- (IBAction)savePhotoButton:(id)sender {
    [DIOSUser userUpdate:userData
                 success:^(AFHTTPRequestOperation *op, id response) {
                      NSLog(@"User data updated!");
                      self.activityInd.hidden = YES;
                      [self.activityInd stopAnimating];

                      [DIOSUser userGet:userData
                                success:^(AFHTTPRequestOperation *op, id response) {
                                    [[DIOSSession sharedSession] user] = userData;
                                    NSLog(@"USER DATA GOTTEN %@", userData);

                                    NSString *imageUpdate = userData[@"field_photo_path"][@"und"][0][@"value"];
                                    NSLog(@"IMAGE STRING %@", imageUpdate);

                                    NSURL *imageUrl = [NSURL URLWithString:imageUpdate];
                                    [self.imageView setImageWithURL:imageUrl];
                                     NSLog(@"IMAGE URL %@", imageUrl);
                                }
                                failure:^(AFHTTPRequestOperation *op, NSError *err) {
                                     NSLog(@"User data failed to update!");
                                 }];
                  }
                  failure:^(AFHTTPRequestOperation *op, NSError *err) {
                       NSLog(@"User data failed to update!");
                  }];

}

您最初设置[[DIOSSession sharedSession] user]的位置或首先设置userData的位置。你能展示一下代码吗?

从源代码我看不到userData上的userGet:会自动更新。我原以为您需要将userData设置为response中返回的数据。你能做一个NSLog(@"Response %@", response);并查看它是否与当前的userData匹配吗?