簡體   English   中英

在 IOS 中高效地將 JSON 解析為 Realm DB(Objective-C)

[英]Parse JSON to Realm DB efficiently in IOS(Objective-C)

我是 Realm 和 Objective-C 的新手。 我已經有一個應用程序可以讀取 JSON 數組並解析為 Realm。 工作正常,但需要 2:30 分鍾來解析超過 20.000 個對象。 我需要在更短的時間內進行解析。

這是我的 JSON 結構:

    {"resultados":[
  {
    "id": 1,
    "tipo": 9,
    "titulo": "name tittle curso",
    "id_padreactividad": 0,
    "hora": "16:55-20:30",
    "fecha": "15/02/2015",
    "acreditado": "Sí",
    "num_creditos": 0.5,
    "ubicacion": 2,
    "tema": "null",
    "patrocinadorId": 0
  },
  {
    "id": 2,
    "tipo": 16,
    "titulo": "Apertura e Introducción\n",
    "id_padreactividad": 1,
    "hora": "16:55-17:00",
    "fecha": "15/02/2015",
    "num_creditos": 0.0,
    "ubicacion": 2,
    "tema": "null",
    "patrocinadorId": 0,
    "descripcion": "null"
  },ect...

這是我從 JSON 解析到領域的代碼

//obtenemos los datos del json con esta simple estructura
    NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
                              [NSURL URLWithString:@"String-for-http-direction-to-json"]];

    NSError *error;
    //hacemos el parseo del json, el error está creado por si fallara para que no siga
    NSMutableDictionary *allCourses = [NSJSONSerialization
                                       JSONObjectWithData:allCoursesData
                                       options:NSJSONReadingMutableContainers
                                       error:&error];

    if( error )
    {
        NSLog(@"%@", [error localizedDescription]);
    }
    else {
        NSArray *resultado = allCourses[@"resultados"];
        total=[resultado count];


        for ( NSDictionary *theCourse in resultado )
        {
            // NSLog(@"Insertando actividad...%d",contador);
            NSLog(@"%d/%d",progress,total);
             contador=contador+1;


            Objeto=[[ActividadBean alloc] init];

            Objeto.id_act = [theCourse[@"id"] intValue];
            Objeto.tipo = [theCourse[@"tipo"]intValue];
            Objeto.titulo = theCourse[@"titulo"];
            Objeto.id_padreactividad = [theCourse[@"id_padreactividad"]intValue];
            Objeto.hora = theCourse[@"hora"];
            Objeto.fecha = theCourse[@"fecha"];
            Objeto.acreditado = theCourse[@"acreditado"];
            Objeto.num_creditos = [theCourse[@"num_creditos"] floatValue];
            Objeto.ubicacion = [theCourse[@"ubicacion"] intValue];
            Objeto.tema = theCourse[@"tema"];
            Objeto.patrocinadorId=[theCourse[@"patrocinadorId"]intValue];
            //guardamos el objeto
            [Objeto save];
        }
    }

這個工作正常,所有導入都沒有問題,但需要一些時間(超過 20000 次解析需要 2:30 分鍾)我知道 java 有方法“createAllFromJson”,但我不知道 IOS 是否有類似的東西。

您的代碼的哪一部分僅用於構建allCourses字典? 目前尚不清楚您是從本地還是遠程源獲取 JSON,因此這可能會導致這個長度過程。

如果 JSON 反序列化花費大量時間,您可以考慮使用替代 JSON 解析器,或更高效的數據格式(如 Realm 或 BSON)。

在您的示例中還有一個對[Objeto save]的調用,我懷疑它會為 20.000 集合中的每個項目創建一個新的寫入事務,這在 Realm 中具有顯着的開銷。 相反,您應該利用 Realm 的事務寫入模型並在單個寫入事務中寫入所有 20.000 個項目,這將加快 Realm 部分的速度。


我強烈建議您使用 Instruments.app 中包含的“時間分析器”來分析您的代碼花費大部分時間的位置。 這將在未來節省您的時間,而不是要求 Stack Overflow 上讓陌生人在互聯網上猜測您的代碼可能會在哪里花費時間;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM