【问题标题】:In iOS if Model notifies View, does this break MVC?在 iOS 中,如果 Model 通知 View,这会破坏 MVC 吗?
【发布时间】:2016-03-10 08:02:45
【问题描述】:

当我创建用户(模型)对象时,我正在使用 SDWebImage(在我的模型中)异步加载 UITableViewCells 中的用户图像。下载图像时,我在 tableviewcell(View) 中显示 NSActivityIndi​​cator 代替图像。当图像被下载时,我重新加载我的表格视图。因为我将这些 NSActivityIndi​​cators 放在了我的单元格中,所以我没有其他方法可以阻止它们,只能通过通知(在 SDWEbimage 方法完成处理程序内)。 所以问题是根据 MVC 架构,这是否是正确的方法?

这是我的模型类(员工)的 sn-p:

    self.employeeID = [self stringValueForKey:@"id"];
    self.name = [self stringValueForKey:@"name"];
    self.company = [self stringValueForKey:@"company"];
    self.companyID = [self numberValueForKey:@"companyId"];
    self.photo = [UIImage imageNamed:@"photoPlaceholder"];

    // fetch Emploee photo
    NSString *photoURL = [NSString stringWithFormat:@"http://blabla.com//img/photo/%@.png",self.employeeID];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:photoURL]
                                                          options:0
                                                         progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
     } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
     {
         if (image && finished)
         {
             self.photo = image;

             [[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGE_FINISHED_DOWNLOADING" object:self];

             // make updates to database from Main Thread to avoid race conditions
             dispatch_async(dispatch_get_main_queue(), ^{

                NSFetchRequest *request = [NSFetchRequest new];
                [request setEntity:[NSEntityDescription entityForName:@"EmployeeCD" inManagedObjectContext:[AppDelegate getDelegate].managedObjectContext]];
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"employeeID == %@",self.employeeID];
                [request setPredicate:predicate];

                NSError *err;
                NSArray *arr = [[AppDelegate getDelegate].managedObjectContext executeFetchRequest:request error:&err];
                if ([arr count]) {
                    ((EmployeeCD*)arr.lastObject).photo = UIImagePNGRepresentation(image);
                    [[AppDelegate getDelegate] saveContext];
                }
            });
         }
         else
         {
             UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
             UIImage *img = [UIImage imageFromColor:[UIColor groupTableViewBackgroundColor]];
             UIImage *scaledImage = [img imageByScalingAndCroppingForSize:CGSizeMake(200, 200)];
             UIImage *roundImage = [UIImage roundImage:scaledImage imageView:imgView];
             self.photo = roundImage;

             [[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGE_COULDNT_DOWNLOAD" object:self];
         }
     }];
}

【问题讨论】:

    标签: ios objective-c uitableview model-view-controller sdwebimage


    【解决方案1】:

    不,你不能,你违反了 iOS MVC 规则 MVC in iOS

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 1970-01-01
      • 2010-11-04
      • 2011-08-14
      • 2020-03-28
      • 2019-10-16
      • 1970-01-01
      • 2018-08-26
      • 2017-06-13
      相关资源
      最近更新 更多