【发布时间】:2016-05-04 09:34:19
【问题描述】:
我是 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 是否有类似的东西。
【问题讨论】:
标签: ios objective-c json parsing realm