发布时RefreshControl不顺畅

时间:2016-03-17 12:46:43

标签: ios swift alamofire moya

我正在尝试将refreshControl连接到Rest api,这样每次刷新它都会更新内容。然而,即使它工作,当你释放refreshControl时,它似乎会导致非常迟缓和不顺畅的体验。那是为什么?

刷新时调用的

方法

func proposeAccess(refresh: Bool,
    success doneCallback:() -> Void
    ) {
    proposeToAccess(.Always, agreed: {

        LocationService.turnOn()

        //Get Latest position
        LocationService.sharedManager.afterUpdatedLocation = { newLocation in

            //Turn off Update User Location
            LocationService.turnOff()

            self.lastLocation = newLocation
            self.updateOrganizations(refresh)


        }
        doneCallback()

        }, rejected: {
            self.alertCanNotAccessLocation()
            doneCallback()
    })
}

建议访问在refreshTableView中调用的位置服务

func updateOrganizations(refresh: Bool) {
    let realm = try! Realm()


    GetOrganization.request(String(self.lastLoadedPage), limit: String(limit), location: self.lastLocation!, radius: String(100), refresh: refresh,
    success: { numberOfResults in


        //Sort by distance
        self.organizationArray = GetOrganization.sortOrganizationsByDistanceFromLocation(realm.objects(Organization), location: self.lastLocation!)


        print(self.lastLoadedPage)
       self.lastLoadedPage = self.lastLoadedPage + 1


    }, error: {
        self.organizationArray = GetOrganization.sortOrganizationsByDistanceFromLocation(realm.objects(Organization), location: self.lastLocation!)


    })


}

在ProposeAccess中调用的UpdateOrganizations

static func request(
lastPage: String,
limit: String,
location: CLLocation,
radius: String,
refresh: Bool,
success successCallback:() -> Void,
error errorCallback:() -> Void
) {

    Provider.request(.Organizations(lastPage, limit,location.coordinate.longitude, location.coordinate.latitude, radius)) { result in
        switch result {
        case let .Success(response):

            do {
                try response.filterSuccessfulStatusCodes()
                let data = try JSON(response.mapJSON())



                let orgArray = data["organizations"]
                let scale = UIScreen.mainScreen().scale
                let cache = Shared.dataCache


                let realm = try! Realm()

                if refresh == true {
                    clearOrganizations()
                }

                try! realm.write {

                    for (_, item) in orgArray {

                        if let id = item["id"].int,
                            let address = item["address"].string,
                            let zipCode = item["zip_code"].string,
                            let name = item["name"].string,
                            let longitude = item["longitude"].double,
                            let latitude = item["latitude"].double,
                            let logoArray = item["logo"].dictionary,
                            let coverPhotoArray = item["cover_photo"].dictionary

                        {
                            var logo: String?
                            var coverPhoto: String?

                            switch scale {
                            case 1.0:
                                logo = logoArray["small"]!.string
                                coverPhoto = coverPhotoArray["small"]!.string
                            case 2.0:
                                logo = logoArray["medium"]!.string
                                coverPhoto = coverPhotoArray["medium"]!.string
                            case 3.0:
                                logo = logoArray["large"]!.string
                                coverPhoto = coverPhotoArray["large"]!.string
                            default:
                                break
                            }

                            if let logoUrl = NSURL(string: logo!),
                                let coverPhotoUrl = NSURL(string: coverPhoto!) {

                                    if let logoData = NSData(contentsOfURL: logoUrl),
                                        let coverPhotoData = NSData(contentsOfURL: coverPhotoUrl) {

                                            let logoCache = "logo-\(id)"
                                            let coverPhotoCache = "cover-\(id)"

                                            cache.set(value: logoData, key: logoCache)
                                            cache.set(value: coverPhotoData, key: coverPhotoCache)



                                            let organization = Organization()
                                            organization.id = id
                                            organization.name = name
                                            organization.address = address
                                            organization.zipCode = zipCode
                                            organization.longitude = longitude
                                            organization.latitude = latitude
                                            organization.logo = logoCache
                                            organization.coverPhoto = coverPhotoCache


                                            realm.add(organization, update: true)






                                    } else {
                                        //return error

                                    }



                            } else {
                                //return error

                            }




                        } else {
                            //return error

                        }

                    }
                }
                successCallback()




            }
            catch {
                errorCallback()
            }



        case .Failure(_):
            errorCallback()

        }
    }
}

在UpdateOrganization中调用GetOrganization请求

XDocument xml = XDocument.Load(file.FullName);
var q = from p in xml.Descendants("xyz")
        select new abc()

1 个答案:

答案 0 :(得分:1)

考虑做这样的事情

            self.refreshControl.performSelector(#selector(UIRefreshControl.endRefreshing), withObject: nil, afterDelay: 0.00)

我尝试使用下面的缩写签名但未获得所需的结果

            self.refreshControl.performSelector(#selector(UIRefreshControl.endRefreshing))

此外,使用iPhone 5模拟器似乎每时每刻表都没有完全收回,但我不知道这是否是模拟器问题。我无法在手机上重现它。