从webservice获取数据确实很慢

时间:2012-10-26 13:55:04

标签: objective-c ios database core-data fetch

我有以下webserivce。

"club": {

    "staff": [ … ],
    "trainingen": [ … ],
    "team": [
        {
            "name": "Baerts Lennert",
            "number": "0",
            "position": "Doelman",
            "birthdate": "1996-09-01",
            "red_cards": 0,
            "yellow_cards": 0,
            "news": [
                {
                    "nid": "1158",
                    "player_id": 1
                },
                {
                    "nid": "1148",
                    "player_id": 1
                },

            ]
        },

我通过以下方法得到这个。

+ (NSArray *)getPlayNewsWithIndex:(int)index
{
    NSLog(@"index is hier %d",index);
    NSString *request = [NSString stringWithFormat:@"http://www.krcgenk.be/mobile/json/request/club/type/ipad"];
    NSLog(@"log1");
    NSArray *arr = [[self executeGenkFetch:request] valueForKeyPath:@"club.team"];
    NSLog(@"log2");
    NSArray *arr2 = [[arr objectAtIndex:index] valueForKey:@"news"];
    NSLog(@"log3");
    return arr2;
}

记录“log1”后。功能继续需要一段时间。我现在的问题是,这怎么可能?

修改

这是我的executeGenkFetch

+ (NSDictionary *)executeGenkFetch:(NSString *)query
{
    query = [NSString stringWithFormat:@"%@", query];
    query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // NSLog(@"[%@ %@] sent %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), query);
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
    if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
    // NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
    return results;
}

0 个答案:

没有答案