微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有Alamofire冻结UI的Swift 4 Realm

如何解决带有Alamofire冻结UI的Swift 4 Realm

我构建了一个函数,该函数使用Alamofire从API下载数据并将其存储在Realm数据库中。 API计数为5k locations25k reviews25k 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 举报,一经查实,本站将立刻删除。