更新するたびにコンテンツが更新されるように、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()
}
}
}