问题描述
我构建了一个函数,该函数使用Alamofire从API下载数据并将其存储在Realm数据库中。 API计数为5k locations
,25k reviews
和25k photos
。函数downloadLocations()
在Swift didFinishLaunchingWithOptions()
我的问题:
一切正常,但启动应用程序约5秒钟后,整个应用程序冻结。 realm.write
完成后,一切都会恢复。 downloadLocations()
应该在没有任何UI限制的情况下在后台运行。这是可能的,还是用Realm数据库的这种数据量正常吗?
func downloadLocations() {
dispatchQueue.main.async {
autoreleasepool {
let url = "\(self.baseUrl)/locations"
let realm = try! Realm()
AF.request(url).responseJSON { (responseData) -> Void in
switch responseData.result {
case let .success(value):
let json = JSON(value)
if let apiData = json["locations"].arrayObject {
try! realm.write {
for loc in apiData as! [[String: AnyObject]] {
if let location = realm.objects(Location.self).filter("id = %@",loc["id"]!).first {
if(location.updated_at != loc["updated_at"] as? String) {
location.title = loc["title"] as? String ?? ""
location.address = loc["address"] as? String ?? ""
location.updated_at = loc["updated_at"] as! String
realm.add(location,update: .modified)
if let reviews = loc["reviews"] {
for rev in reviews as! [[String: AnyObject]] {
if let review = realm.objects(Review.self).filter("id = %@",rev["id"]!).first {
review.location_id = loc["id"] as! Int
review.author = rev["author"] as? String ?? ""
review.review = rev["review"] as? String ?? ""
review.rating = rev["rating"] as? Int ?? 0
review.time = rev["time"] as? Int ?? 0
realm.add(review,update: .modified)
} else {
let review = Review()
review.id = rev["id"] as! Int
review.location_id = loc["id"] as! Int
review.author = rev["author"] as? String ?? ""
review.review = rev["review"] as? String ?? ""
review.rating = rev["rating"] as? Int ?? 0
review.time = rev["time"] as? Int ?? 0
realm.add(review)
}
}
}
if let photos = loc["photos"] {
for phto in photos as! [[String: AnyObject]] {
if let photo = realm.objects(Photo.self).filter("id = %@",phto["id"]!).first {
photo.location_id = loc["id"] as! Int
photo.photo_url = phto["photo_url"] as? String ?? ""
realm.add(photo,update: .modified)
} else {
let photo = Photo()
photo.id = phto["id"] as! Int
photo.location_id = loc["id"] as! Int
photo.photo_url = phto["photo_url"] as? String ?? ""
realm.add(photo)
}
}
}
}
} else {
let location = Location()
location.id = loc["id"] as! Int
location.title = loc["title"] as? String ?? ""
location.address = loc["address"] as? String ?? ""
location.updated_at = loc["updated_at"] as! String
realm.add(location)
if let reviews = loc["reviews"] {
for rev in reviews as! [[String: AnyObject]] {
if let review = realm.objects(Review.self).filter("id = %@",rev["id"]!).first {
review.location_id = loc["id"] as! Int
review.author = rev["author"] as? String ?? ""
review.review = rev["review"] as? String ?? ""
review.rating = rev["rating"] as? Int ?? 0
review.time = rev["time"] as? Int ?? 0
realm.add(review,update: .modified)
} else {
let review = Review()
review.id = rev["id"] as! Int
review.location_id = loc["id"] as! Int
review.author = rev["author"] as? String ?? ""
review.review = rev["review"] as? String ?? ""
review.rating = rev["rating"] as? Int ?? 0
review.time = rev["time"] as? Int ?? 0
realm.add(review)
}
}
}
if let photos = loc["photos"] {
for phto in photos as! [[String: AnyObject]] {
if let photo = realm.objects(Photo.self).filter("id = %@",phto["id"]!).first {
photo.location_id = loc["id"] as! Int
photo.photo_url = phto["photo_url"] as? String ?? ""
realm.add(photo,update: .modified)
} else {
let photo = Photo()
photo.id = phto["id"] as! Int
photo.location_id = loc["id"] as! Int
photo.photo_url = phto["photo_url"] as? String ?? ""
realm.add(photo)
}
}
}
}
}
}
}
case let .failure(error):
print(error)
}
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)