【问题标题】:MKNetworkEngine doesn't fire its methodsMKNetworkEngine 不会触发它的方法
【发布时间】:2014-01-11 20:50:44
【问题描述】:

我正在尝试将一组图像从 Web 服务加载到 UICollectionView,并且我正在使用 MKNetworkKit 来处理网络操作。

这有点奇怪,因为它在一种情况下有效,而在另一种情况下无效。请多多包涵,这篇文章有点长。

我有一个简单的故事板应用程序,其中包含一个 UIViewController,其中嵌入了一个 UICollectionView 作为其主视图(由于 UI 更改,我将稍后再做,因此无法使用 UICollectionViewController )。

我创建了一个类,它是MKNetworkEngine 的子类,用于处理从 Web 服务检索图像的方法。

ImageEngine.h

#import "MKNetworkEngine.h"

@interface ImageEngine : MKNetworkEngine

typedef void (^ImagesResponseBlock)(NSMutableArray *imageURLs);

- (void)allImages:(ImagesResponseBlock)imageURLBlock errorHandler:(MKNKErrorBlock)errorBlock;

@end

ImageEngine.m

#import "ImageEngine.h"

@implementation ImageEngine

- (void)allImages:(ImagesResponseBlock)imageURLBlock errorHandler:(MKNKErrorBlock)errorBlock
{
    MKNetworkOperation *op = [self operationWithPath:@"All_Images.php"];

    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        [completedOperation responseJSONWithCompletionHandler:^(id jsonObject) {
            imageURLBlock(jsonObject[@"Images"]);
        }];
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        errorBlock(error);
    }];
    [self enqueueOperation:op];
}

- (NSString *)cacheDirectoryName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *cacheDirectoryName = [documentsDirectory stringByAppendingPathComponent:@"SomeImages"];

    return cacheDirectoryName;
}

@end

在带有集合视图的视图控制器类中,

#import "AppDelegate.h"
#import "GridViewController.h"
#import "ImageCell.h"

@interface GridViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *images;
@property (strong, nonatomic) NSString *selectedImageUrl;

@end

@implementation GridViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    [ApplicationDelegate.imageEngine allImages:^(NSMutableArray *images) {
        self.images = images;
        [self.collectionView reloadData];
    } errorHandler:^(NSError *error) {
        NSLog(@"MKNetwork Error: %@", error.localizedDescription);
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *thisImage = self.images[indexPath.row];
    self.selectedImageUrl = [NSString stringWithFormat:@"%@%@", @"http://toonmoodz.osmium.lk/", thisImage[@"image"]];
    [cell.imageView setImageFromURL:[NSURL URLWithString:self.selectedImageUrl]];

    return cell;
}

@end

这很好用。图像按预期加载。

然后我使用MBPullDownController 对应用程序进行了小的 UI 升级。它只是在集合视图下添加了一个表格视图。网络代码没有变化。只是 MBPullDownController 的一个新子类,用于在主视图控制器中嵌入集合视图和表格视图。

但是当我这样做时,图像根本不会加载。我在 ImageEngine 类的方法中放置了一个断点,以查看它们是否被触发,但它从来没有发生过。 (奇怪的是这段代码今天早上实际上运行良好。现在它没有,我完全不知道为什么)。它也不会抛出任何错误或警告。

我已经上传了两个项目来演示这个问题,以便其他人更容易理解。如果有人可以帮助我,我将不胜感激。在过去的几个小时里,我一直在努力解决这个问题。

Source正常工作的版本。

This 是有问题的项目的来源。 (当你运行它时,它会显示一个空白的白色视图。它看起来像一个普通视图,但它是集合视图。我加载了一组本地图像以查看它是否正常工作)

谢谢。

【问题讨论】:

  • 显示空白视图是正常的,因为没有集合视图或“MKNetworkEngine”(至少在第二个链接“with the issue”中。
  • 有一个集合视图。 See。底部的两个视图使用情节提要 ID 以编程方式加载到顶部视图控制器中。而MKNetworkEngine的子类在项目中称为ImageEngine。
  • @TonyMkenu 哦等等!你是对的。我上传了一个错误的项目。感谢您注意到这一点。链接已更新。
  • 是的,我的错。对于那个很抱歉。我更新了链接:)

标签: ios ios7 uicollectionview mknetworkkit mknetworkengine


【解决方案1】:

当我在viewDidLoad 方法中的GridViewController 中设置断点时,在控制台中执行po [[UIApplication sharedApplication] valueForKeyPath:@"delegate.imageEngine"] 后,我看到imageEngine 属性等于nil。

看起来application:didFinishLaunchingWithOptionsGridViewController 中的viewDidLoad 之后执行。

我从您的application:didFinishLaunchingWithOptions 中删除了这两行:

self.imageEngine = [[ImageEngine alloc] initWithHostName:@"toonmoodz.osmium.lk"];
[self.imageEngine useCache];

然后我将 imageEngine 惰性加载器添加到 AppDelegate 和它的工作 http://cl.ly/image/1S172D2e050J

- (ImageEngine*) imageEngine {
    if (_imageEngine == nil) {
        _imageEngine = [[ImageEngine alloc] initWithHostName:@"toonmoodz.osmium.lk"];
        [_imageEngine useCache];
    }
    return _imageEngine;
}

【讨论】:

  • 谢谢你,luleq。您的解决方案有效!我还找到了另一种方法。我将viewDidLoad 中的代码部分移动到viewWillAppear,现在它也可以正常工作了。感谢您花时间解决它:)
猜你喜欢
  • 1970-01-01
  • 2012-11-07
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2014-02-22
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多