RefreshControl 释放时不流畅
Posted
技术标签:
【中文标题】RefreshControl 释放时不流畅【英文标题】:RefreshControl not smooth when release 【发布时间】:2016-03-17 12:46:43 【问题描述】:我正在尝试将 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()
【问题讨论】:
【参考方案1】:考虑做这样的事情
self.refreshControl.performSelector(#selector(UIRefreshControl.endRefreshing), withObject: nil, afterDelay: 0.00)
我尝试使用下面的缩写签名,但没有得到想要的结果
self.refreshControl.performSelector(#selector(UIRefreshControl.endRefreshing))
另外,使用 iPhone 5 模拟器时,桌子似乎不时完全缩回,但我不知道这是否是模拟器问题。我无法在手机上复制它。
【讨论】:
哇哇哇哇哇哇哇哇。惊人的!为什么它有效?我在 *** 上尝试了很多解决方案,但是这一行就像一个魅力!谢啦兄弟! Xcode 9.0, swift 4.0 - “endRefreshing”的平滑动画 - 工作!以上是关于RefreshControl 释放时不流畅的主要内容,如果未能解决你的问题,请参考以下文章
为啥这个 ObjC 块在释放时不释放其捕获的引用?包括失败的单元测试
Gio.MemoryInputStream 关闭时不释放内存