1

我正在尝试向NSCache我的子MKTileOverlay类添加一个:

@interface WMSTileOverlay ()

@property NSCache *cache;

@end

@implementation WMSTileOverlay

-(instancetype)initWithURLTemplate:(NSString *)URLTemplate {
    self = [super initWithURLTemplate:URLTemplate];

    if (self) {
        DLog(@"setting cache");
        self.cache = [[NSCache alloc] init];
        DLog(@"original self.cache: %@", self.cache);

    }

    return self;
}
-(void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *, NSError *))result {
if (!result) {
    return;
}

NSData *cachedData = [self.cache objectForKey:[self URLForTilePath:path]];
if (cachedData) {
    DLog(@"Cached tile found!!!!!!!");
    result(cachedData, nil);
} else {

    NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) {
            result (nil, connectionError);
        }
        else {
            DLog(@"adding data to cache for tile %@", [self URLForTilePath:path]);
            DLog(@"cache: %@", self.cache);
            [self.cache setObject:data forKey:[self URLForTilePath:path]];

            if ([self.cache objectForKey:[self URLForTilePath:path]]) {
                DLog(@"found the data for url path");
            }
            result (data, nil);
        }
    }];
}

}

但是我从来没有看到从缓存中获取切片(我从来没有看到日志消息“找到缓存切片!!!!!!”)。

我可能做错了什么?

编辑:我添加了日志信息

我添加了一堆日志记录。我检查是否创建了缓存,以及它是否与我在查找图块时引用的缓存相同。我还检查了数据是否已添加到缓存中,并在添加后立即找到:

DEBUG | -[WMSTileOverlay initWithMapContext:andLayer:] | original self.cache: <NSCache: 0x1bf59e10>
DEBUG | __40-[WMSTileOverlay loadTileAtPath:result:]_block_invoke | adding data to cache for tile service=wms&version=1.1.1&request=GetMap&SRS=EPSG:4326&layers=displayimg&mode=tiled&tile=30295+46150+17&format=image/png&transparent=true
DEBUG | __40-[WMSTileOverlay loadTileAtPath:result:]_block_invoke | cache: <NSCache: 0x1bf59e10>
DEBUG | __40-[WMSTileOverlay loadTileAtPath:result:]_block_invoke | found the data for url path
4

1 回答 1

0

这是因为一旦加载了特定路径的图块,MapKit 就不会在同一会话中再次为该路径调用 loadTileAtPath,我认为 MapKit 已经以某种方式在内部缓存了加载的图块。

于 2016-02-20T07:45:31.373 回答