我们的Swift应用程序获取具有15.000个对象(并在不断增加)的相对较大的JSON数据集,使用对象ObjectMapper库进行对象映射,然后将这些对象添加到Realm数据库中。
但是,对象映射和将对象添加到Realm需要花费一些时间-在最快的iPhone上需要15秒。即使试图跳过繁琐的日期/时间转换操作,它仍然需要12秒钟来处理。
由于映射的对象用于在地图上显示对象,因此用户等待15秒才能看到屏幕是不可接受的。
为了改善用户体验,我们在应用启动时获取JSON数据,然后调用背景优先级最低的对象映射以为用户提供无缝的体验,因此在创建用户时要进行繁重的操作一个帐户。
我想在更智能地获取和解析数据方面进一步优化此设置。您对此有何建议?
这是Realm扩展中用于映射和添加到数据库的方法,其中Mapper<T>().mapArray(JSONArray: json)
直接调用ObjectMapper的映射方法:
/**
Import JSON as array of BaseMappable Object.
- parammeter T: BaseMappable Object.
- parameter type: mapped type.
- parameter json: JSON type is [[String: Any]].
- returns: mapped objects.
*/
@discardableResult
public func map<T: Object>(_ type: T.Type, json: [[String: Any]], update: Bool = false) -> [T] where T: BaseMappable {
let objs = Mapper<T>().mapArray(JSONArray: json)
add(objs, update: update)
return objs
}
这是Profiler中最重的堆栈跟踪(具有过滤的系统调用):
40 14919.0 MyAwesomeApplication(6446):0 39 14130.0 _dispatch_workloop_worker_thread $ VARIANT $ armv81 0x2367c7:0 38 libsystem_pthread.dylib 14130.0 _pthread_wqthread 34 libdispatch.dylib 13870.0 _dispatch_call_block_and_release 33 MyAwesomeApplication 13870.0 _T0Ieg_IeyB_TR /Users/blackbeard/Developer/awesomeapp/ios-app/awesomeapp/Library/Core/Router.swift:0 24 MyAwesomeApplication 11562.0 _T012ObjectMapper0B0C11MyAwesomeApplication10RealmSwift0A0CRbzrlE3mapy9Alamofire6ResultOyypG6result_AD8DataTypeO4typeSSSg7withKeySb6updateSb12inBackgroundyALc10completiontF10runMappingL0_yyAGRKz5L 23 RealmSwift 11550.0 Realm.write(_ :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/RealmSwift/RealmSwift/Realm.swift:155 22 MyAwesomeApplication 11550.0 _T012ObjectMapper0B0C11MyAwesomeApplication10RealmSwift0A0CRbzrlE3mapy9Alamofire6ResultOyypG6result_AD8DataTypeO4typeSSSg7withKeySb6updateSb12inBackgroundyALc10completiontF10runMappingL0_yyAGRKz5lCg_cf_yyAGRBzTA12L 20 MyAwesomeApplication 11550.0 _T012ObjectMapper0B0C11MyAwesomeApplication10RealmSwift0A0CRbzrlE3mapy9Alamofire6ResultOyypG6result_AD8DataTypeO4typeSSSg7withKeySb6updateSb12inBackgroundyALc10completiontF10runMappingL0_yyAGRKz5lCg_Cy_CFA /Users/blackbeard/Developer/awesomeapp/ios-app/awesomeapp/Library/Ext/Mapper.swift:55 19 ObjectMapper 9173.0 Mapper.mapArray(JSONArray :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Mapper.swift:0 15 ObjectMapper 9084.0部分申请了@callee_guaranteed的thunk (@own [String:任何])->(@out A ?, @error @owned错误) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Mapper.swift:0 14 ObjectMapper 9084.0为@callee_guaranteed(@owned [String: 任何])->(@out A ?, @error @owned错误) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Mapper.swift:0 13 ObjectMapper 9068.0 Mapper.map(JSON :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Mapper.swift:106 12 MyAwesomeApplication 8865.0协议见证 一致性存储中的BaseMappable.mapping(map :) /Users/blackbeard/Developer/awesomeapp/ios-app/awesomeapp/Model/Schema/Store/Store.swift:105 11 MyAwesomeApplication 8865.0 Store.mapping(地图:) /Users/blackbeard/Developer/awesomeapp/ios-app/awesomeapp/Model/Schema/Store/Store.swift:0 10 MyAwesomeApplication 8819.0专门的Store.mapping(map :) /Users/blackbeard/Developer/awesomeapp/ios-app/awesomeapp/Model/Schema/Store/Store.swift:0 9 ObjectMapper 5450.0 _T012ObjectMapper3MapCACSScig /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Map.swift:63 8 ObjectMapper 5435.0 Map.subscript(键:嵌套:定界符:ignoreNil :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Map.swift:96 7 ObjectMapper 5435.0专门 Map.subscript(键:嵌套:定界符:ignoreNil :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Map.swift:0 6 ObjectMapper 5070.0专门 Map.subscript(键:嵌套:定界符:ignoreNil :) /Users/blackbeard/Developer/awesomeapp/ios-app/Pods/ObjectMapper/Sources/Map.swift:99 5 libswiftFoundation.dylib 3397.0 _T0s14StringProtocolP10FoundationSS5IndexVADRtzrlE8包含Sbqd__sAARd__lF 0 CoreFoundation 68.0 CFStringGetCStringPtr