如何进行超快速过滤NSArray操作?

时间:2016-02-04 04:45:16

标签: ios objective-c arrays json core-data

我有一种情况,我需要在核心数据中为1000个checkpoint对象创建关系到4000个routeSequence对象,我有这样的JSON RouteSequence 实体:

{
    "checkpoint_id": 506,
    "hash": "f3ab72c1cb65ce73334d4fb257f7ca3d",
    "is_end": true,
    "is_forward_direction": true,
    "next_checkpoint_id": 509,
    "order": 0,
    "path": [
            "65.594608 57.112931",
            "65.591262 57.112920",
            "65.590060 57.112888",
            "65.589605 57.112841",
            "65.589151 57.112748",
            "65.588543 57.112388",
            "65.588526 57.112383",
            "65.588047 57.112037"
    ],
    "route_id": 66
},
{
    "checkpoint_id": 28,
    "hash": "512f248f72e8d46e6a56774e974ea79b",
    "is_end": true,
    "is_forward_direction": true,
    "next_checkpoint_id": 729,
    "order": 0,
    "path": [
            "65.635648 57.113168",
            "65.634405 57.113396",
            "65.634186 57.113431",
            "65.633957 57.113427",
            "65.633744 57.113383",
            "65.633567 57.113305",
            "65.633208 57.113091",
            "65.631777 57.113354"
    ],
    "route_id": 10
},

检查点这样的实体

{
    "city_id": 1,
    "code_number": "03283",
    "description": "---",
    "hash": "eb6b56b6ca2339e6bcd2e31df74de10b",
    "id": 3396,
    "lat": 57.2015298151631,
    "lon": 65.3598978059826,
    "name": "\"Астра\"",
    "routes_ids": [
            61
    ]
},
{
    "city_id": 1,
    "code_number": "03282",
    "description": "---",
    "hash": "68f2a82d3745413662fed4c30eab6b49",
    "id": 3397,
    "lat": 57.20036829793,
    "lon": 65.3599455633935,
    "name": "\"Астра\"",
    "routes_ids": [
            61
    ]
},

我需要做什么?我需要在RouteSequence.checkpoint_id == Checkpoint.id之间创建关系 在创建关系的那一刻,我已经从这个json创建了对象。此时此操作需要15秒。我们的数据每天更新,不允许每天早上等待启动应用程序15秒。

NSArray *checkpoints = [delegate.writerManagedObjectContext executeFetchRequest:[NSFetchRequest fetchRequestWithEntityName:@"Checkpoint"] error:&error];
NSArray *array = [[NSArray alloc] init];
NSMutableArray *routeSequences = [[[delegate.writerManagedObjectContext executeFetchRequest:[NSFetchRequest fetchRequestWithEntityName:@"RouteSequence"] error:&error] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"checkpointID" ascending:YES]]] mutableCopy];
        for (Checkpoint *checkpoint in checkpoints) {
            NSLog(@"%i", routeSequences.count);
            array = [routeSequences filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"checkpointID == %@", checkpoint.id]];
            [checkpoint addRouteSequences:[NSSet setWithArray:array]];
            [routeSequences removeObjectsInArray:array];
        }

目前临时解决方案是在后台进行此操作,但我们不希望这样,我们想要做得更好,所以问题 - 如何加快这个操作。也许我们可以用干净的C来做到这一点?

0 个答案:

没有答案