我正在尝试将 refreshControl 连接到 Rest api,以便每次刷新时都会更新内容。然而,即使它有效,当您释放 refreshControl 时,它似乎会导致非常滞后且不流畅的体验。这是为什么?
刷新时调用的方法
func refreshTableView() {
self.lastLoadedPage = 1
proposeAccess(true, success: {
self.refresher?.endRefreshing()
})
}
提议访问在 refreshTableView 中调用的位置服务
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()
})
}
在 ProposeAccess 中调用的 UpdateOrganizations
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!)
})
}
在 UpdateOrganization 中调用 GetOrganization 请求
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()
}
}
}