带阿拉莫菲尔冻结用户界面的Swift 4领域

带阿拉莫菲尔冻结用户界面的Swift 4领域,swift,realm,alamofire,swift5,swifty-json,Swift,Realm,Alamofire,Swift5,Swifty Json,我构建了一个函数,可以使用Alamofire从API下载数据并将其存储在领域数据库中。API统计了5k个位置,25k个评论和25k张照片。在SwiftdidFinishLaunchingWithOptions()中设置函数downloadLocations() 我的问题是: 一切正常,但当启动应用程序约5秒时,整个应用程序都冻结了。当realm.write完成时,一切都会恢复正常。downloadLocations()应该在后台运行,没有任何UI限制。这是可能的,还是对于一个领域数据库来说这是正

我构建了一个函数,可以使用Alamofire从API下载数据并将其存储在领域数据库中。API统计了
5k个位置
25k个评论
25k张照片
。在Swift
didFinishLaunchingWithOptions()中设置函数
downloadLocations()

我的问题是:

一切正常,但当启动应用程序约5秒时,整个应用程序都冻结了。当
realm.write
完成时,一切都会恢复正常。
downloadLocations()
应该在后台运行,没有任何UI限制。这是可能的,还是对于一个领域数据库来说这是正常的

func下载位置(){
DispatchQueue.main.async{
自动释放池{
let url=“\(self.baseUrl)/位置”
让realm=try!realm()
AF.request(url).responseJSON{(responseData)->Void in
开关响应数据结果{
成功案例(价值):
让json=json(值)
如果让apiData=json[“位置”].arrayObject{
试试看!写吧{
对于apiData as中的loc![[String:AnyObject]]{
如果让location=realm.objects(location.self).filter(“id=%@”,loc[“id”!)。首先{
if(location.updated_at!=loc[“updated_at”]as?字符串){
location.title=loc[“title”]作为?字符串??“
location.address=loc[“address”]作为?字符串??“
location.updated_at=loc[“updated_at”]as!String
realm.add(位置,更新:。已修改)
如果let reviews=loc[“reviews”]{
以![[String:AnyObject]]的形式发布评论{
如果让review=realm.objects(review.self).filter(“id=%@”,rev[“id”!)。首先{
review.location_id=loc[“id”]as!Int
review.author=rev[“author”]作为?字符串??“
review.review=rev[“review”]作为?字符串??“
review.rating=rev[“rating”]as?Int??0
review.time=rev[“time”]as?Int±0
realm.add(查看、更新:。已修改)
}否则{
让我们回顾一下
review.id=rev[“id”]as!Int
review.location_id=loc[“id”]as!Int
review.author=rev[“author”]作为?字符串??“
review.review=rev[“review”]作为?字符串??“
review.rating=rev[“rating”]as?Int??0
review.time=rev[“time”]as?Int±0
realm.add(查看)
}
}
}
如果let photos=loc[“photos”]{
用于照片中的phto![[String:AnyObject]]{
如果让photo=realm.objects(photo.self).filter(“id=%@”,phto[“id”!)。首先{
photo.location_id=loc[“id”]as!Int
photo.photo\u url=phto[“photo\u url”]作为?字符串??“
realm.add(照片,更新:。已修改)
}否则{
让photo=photo()
photo.id=phto[“id”]as!Int
photo.location_id=loc[“id”]as!Int
photo.photo\u url=phto[“photo\u url”]作为?字符串??“
realm.add(照片)
}
}
}
}
}否则{
让位置=位置()
location.id=loc[“id”]as!Int
location.title=loc[“title”]作为?字符串??“
location.address=loc[“address”]作为?字符串??“
location.updated_at=loc[“updated_at”]as!String
realm.add(位置)
如果let reviews=loc[“reviews”]{
以![[String:AnyObject]]的形式发布评论{
如果让review=realm.objects(review.self).filter(“id=%@”,rev[“id”!)。首先{
review.location_id=loc[“id”]as!Int
复习