对于视图模型中的 swift 引用,自引用应该是弱引用还是未知引用?
For swift references within view model, should self reference be weak or unknown?
我的视图模型中的所有函数都将 self 引用为弱引用,但是我刚刚读到“如果捕获的引用永远不会变为 nil,则应始终将其捕获为无主引用,而不是弱引用。” Here is link。这是我的一些代码示例,显然我的视图模型是 class,所有这些功能都在我的视图模型中
(PS,我理解有些代码很糟糕,我想在重构之前减少内存分配):
func fetchUserProfilePhoto(completion: @escaping () -> Void) {
let resource = GetUserProfilePhotoResource(userId: self.userId)
let request = APIRequestSingle(resource: resource)
self.userProfilePhotoRequest = request
request.execute { [weak self] result in
switch result {
case .success(let value):
self?.userProfile?.profilePhotoURL = value.photoUrl
self?.fetchUserProfileImage()
case .failure(let error):
print("user profile image \(error)")
switch error.resolveCategory() {
case .nonRetryable:
self?.userProfilePhotoError = true
self?.retryableError = false
case .retryable:
self?.userProfilePhotoError = true
self?.retryableError = true
case .requiresLogout:
print("User profile photo LOGOUT")
}
case .none:
print("Nothing from fetch User profile Photo")
}
}
}
func postUserProfilePhoto(photo: UIImage) {
guard !userProfilePhotoIsLoading else {return}
userProfilePhotoIsLoading = true
postProfilePhotoPresignedURL(numberOfImages: 1) { [weak self] url in
for i in url {
self?.putUserProfilePhoto(url: i.uploadURL, savedUrl: i.imageURL, profilePhoto: photo) { [weak self] in
self?.postUserProfilePhotoToAPI(photoUrl: i.imageURL) { [weak self] in
self?.fetchUserProfilePhoto() { [weak self] in
self?.userProfilePhotoIsLoading = false
}
}
}
}
}
}
我真的很想确保我以尽可能最好的方式分配内存资源,上面的引述有点让我失望。我错过了什么吗?视图模型中的最佳实践是什么?最后,在闭包中我将 self 设置为可选,我应该强制解包吗?是否存在从内存中释放视图模型的情况?
感谢任何有关该主题的知识,真的很想把它记下来。
你说:
I … was a little thrown off by the quote above. Am I missing something?
weak
和 unowned
的区别仅在于当对象被释放时,它会经过一个额外的步骤并返回 nil
weak
引用,但不是 for unowned
引用。因此,带有 unowned
引用的代码可以稍微高效一些。但是在使用 unowned
时必须小心谨慎,因为如果您在该对象被释放后尝试使用该引用,应用程序将会崩溃。
What are best practices within the view model?
就我个人而言,我会使用 weak
。开销是无关紧要的,异步代码的存在使关于 unowned
引用的推理以及对象是否在到达 unowned
引用时被释放的推理变得非常复杂。
Lastly, within the closure I am setting self to optional, should I force unwrap? Is there any case in which a view model would be deallocated from memory?
您绝对不想在处理异步代码时强制解包。如果视图及其关联的视图模型在异步完成处理程序代码运行时被释放怎么办?!如果你知道它永远不会是 nil
(这里不是这种情况),你应该只强制解包。
我的视图模型中的所有函数都将 self 引用为弱引用,但是我刚刚读到“如果捕获的引用永远不会变为 nil,则应始终将其捕获为无主引用,而不是弱引用。” Here is link。这是我的一些代码示例,显然我的视图模型是 class,所有这些功能都在我的视图模型中
(PS,我理解有些代码很糟糕,我想在重构之前减少内存分配):
func fetchUserProfilePhoto(completion: @escaping () -> Void) {
let resource = GetUserProfilePhotoResource(userId: self.userId)
let request = APIRequestSingle(resource: resource)
self.userProfilePhotoRequest = request
request.execute { [weak self] result in
switch result {
case .success(let value):
self?.userProfile?.profilePhotoURL = value.photoUrl
self?.fetchUserProfileImage()
case .failure(let error):
print("user profile image \(error)")
switch error.resolveCategory() {
case .nonRetryable:
self?.userProfilePhotoError = true
self?.retryableError = false
case .retryable:
self?.userProfilePhotoError = true
self?.retryableError = true
case .requiresLogout:
print("User profile photo LOGOUT")
}
case .none:
print("Nothing from fetch User profile Photo")
}
}
}
func postUserProfilePhoto(photo: UIImage) {
guard !userProfilePhotoIsLoading else {return}
userProfilePhotoIsLoading = true
postProfilePhotoPresignedURL(numberOfImages: 1) { [weak self] url in
for i in url {
self?.putUserProfilePhoto(url: i.uploadURL, savedUrl: i.imageURL, profilePhoto: photo) { [weak self] in
self?.postUserProfilePhotoToAPI(photoUrl: i.imageURL) { [weak self] in
self?.fetchUserProfilePhoto() { [weak self] in
self?.userProfilePhotoIsLoading = false
}
}
}
}
}
}
我真的很想确保我以尽可能最好的方式分配内存资源,上面的引述有点让我失望。我错过了什么吗?视图模型中的最佳实践是什么?最后,在闭包中我将 self 设置为可选,我应该强制解包吗?是否存在从内存中释放视图模型的情况?
感谢任何有关该主题的知识,真的很想把它记下来。
你说:
I … was a little thrown off by the quote above. Am I missing something?
weak
和 unowned
的区别仅在于当对象被释放时,它会经过一个额外的步骤并返回 nil
weak
引用,但不是 for unowned
引用。因此,带有 unowned
引用的代码可以稍微高效一些。但是在使用 unowned
时必须小心谨慎,因为如果您在该对象被释放后尝试使用该引用,应用程序将会崩溃。
What are best practices within the view model?
就我个人而言,我会使用 weak
。开销是无关紧要的,异步代码的存在使关于 unowned
引用的推理以及对象是否在到达 unowned
引用时被释放的推理变得非常复杂。
Lastly, within the closure I am setting self to optional, should I force unwrap? Is there any case in which a view model would be deallocated from memory?
您绝对不想在处理异步代码时强制解包。如果视图及其关联的视图模型在异步完成处理程序代码运行时被释放怎么办?!如果你知道它永远不会是 nil
(这里不是这种情况),你应该只强制解包。