触发通知时不更新 NSString



我的用户可以上传和/或在其个人资料(ViewController)上拍照。也就是说,保存照片后,我希望在保存后立即使用最新照片更新 imageView。为了做到这一点,我需要刷新NSString(secondLink)。从理论上讲,我认为下面的代码会起作用 - 例如触发方法 userpic成功保存后更新(方法从服务器"重新获取"信息)。也就是说,由于某种原因,下面的userpicUpdate方法只是提取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!");
         }
         ];
        }

查看 Github 上的源代码,DIOSUser userUpdate: 不会重新加载用户信息,因此您需要在更新成功时调用 DIOSUser userGet:。成功userGet:,您可以保存返回的用户数据,并通过触发 NSNotificationCenter 来更新UIImage


成功时更新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的位置。你能展示那个代码吗?

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

最新更新