[英]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.