iOS Custom Delegation Delete Posts
Fullstack Social iOS NodeJS REST
To wrap up the delete feature for our custom backend solution, we'll go ahead and implement deleting on our IOS application. First we'll present a confirm modal to show the options for deleting a post. Once the delete option is selected, we can execute our delete request via Alamofire and then remove the post object from our tableview.

Comments (6)
romulusc
4 years ago
Hi Brian, I'm got this strange bug, including when using your app in the project download, i don't know what i'm missing. So after adding fetchPosts() in viewDidLoad, after login posts are not fetched. Manually clicking 'fetch posts' will bring them up ok though. And then even when logged on, every time I restart app, I can only fetch posts manually if I do another login. Again my ios app does not remain logged in after restart. Can't see why yet. Performing login 2019-08-06 16:39:52.007795+0100 fullstacksocial_ios_lbta[2706:483702] [Common] _BSMachError: port b203; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
romulusc
4 years ago
Don't worry, now seems to work, after using not only the ios app but your web app as well from 'download project'...with my credentials.....must have missed something in my version of the web app because is working as it should using yours...cheers
Brian Voong
4 years ago
Brian Voong
4 years ago
romulusc
4 years ago
Thanks Brian, i’ll go back to lesson 10 and my version of the app, thanks a lot.
Bobur Pakhriev
3 years ago
Hi Brian again)) when i tried to login its coming up with this error Failed to hit server: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) and when i try to sign up below error Show login and sign up pages 2019-11-13 12:27:35.252546+0900 websocial[15548:3989737] [AutoFill] Cannot show Automatic Strong Passwords for app bundleID: com.letsbuildthatapp.fullstacksocial-ios-lbta due to error: iCloud Keychain is disabled Failed to sign up: responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(code: 400)) how can be fixed?
AlfieLBTA
3 years ago
Hey Brian nice afternoon. Im trying to refactor the code into a service controller but i don't quite understand how alamofire responses work i have this piece of code but i don't know how to construct the response data type on teh completion block. Can you point towards any video that explains it , i have tried reading Alamo's docs and migration guides, but either one presupposes i already know how it worked on previous versions or the docs are too high lvl for my understanding. This is mi Service function: func deletePost(postId: String, completion: (AFResult<Success>)) -> () { let url = "\(endPoint)/posts/\(postId)" AF.request(url, method: .delete) .validate(statusCode: 200..<300) .responseData { (dataResp) in if let err = dataResp.error { print("Failed to delete", err) completion(.failure(err)) return } completion(.success(_)) } } Thanks in advance.
AlfieLBTA
3 years ago
Got tinkering with it a lil more and made it work with this: func deletePost(postId: String, completionHandler: @escaping (AFResult<Data>) -> ()) { let url = "\(Service.shared.endPoint)/posts/\(postId)" AF.request(url, method: .delete) .validate(statusCode: 200..<300) .responseData { (dataResp) in if let err = dataResp.error { print("Failed to delete", err) completionHandler(.failure(err)) return } completionHandler(.success(dataResp.data ?? Data())) } } Read also some blog posts about closures but i guess is to high lvl for me still.
hamontge1
3 years ago
The image is deleted from the server and status code of 200, but for some reason I am getting this response on Xcode. Once I click fetch post it shows that the image is deleted. Quick workaround is to remove return... but i would like it to work properly. Any ideas? Failed to delete responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) extension HomeController: PostCellOptionsDelegate { func handlePostOptions(cell: PostCell) { guard let indexPath = tableView.indexPath(for: cell) else { return } let post = self.posts[indexPath.row] let url = "\(Service.shared.baseUrl)/post/\(post.id)" AF.request(url, method: .delete) .validate(statusCode: 200..<300) .responseData { (dataResp) in if let err = dataResp.error { print("Failed to delete", err) return //https://stackoverflow.com/questions/46284163/alamofire-responseserializationfailedalamofire-aferror-responseserializationfai } self.posts.remove(at: indexPath.row) self.tableView.deleteRows(at: [indexPath], with: .automatic) } } }
Udin Rajkarnikar
3 years ago
Instead of "...validate(statusCode: 200..<300).responseData" change ..validate(statusCode: 200..<300).response { completion handler } and it should work fine.
omq78
3 years ago
thanks you
Lazaro Ambrosio
2 years ago
Thanks Udin for the help.
Juric Daniel
3 years ago
Hey Brain, I have got this strange thing. Whenever I press the option button to delete the post, the row of the tableView got selected instead of presenting UIAlertController. Can you please answer why?
Juric Daniel
3 years ago
I just got it. :)
Christopher J. Roura
2 years ago
What was your solution for this?
Juric Daniel
2 years ago
I added this “contentView.isUserInteractionEnabled = false” in the PostCell.
Christopher J. Roura
2 years ago
Thank you
Juric Daniel
2 years ago
You’re welcome :)
omq78
2 years ago
thank you
Christopher J. Roura
2 years ago
Here is an extension to fix the auto layout bug with the alert sheet. This error has been around since iOS 12 and still has yet to be addressed by Apple. In the mean time add this extension function right after all of the add actions and right before you present the alert sheet. extension UIAlertController { func pruneNegativeWidthConstraints() { for subView in self.view.subviews { for constraint in subView.constraints where constraint.debugDescription.contains("width == - 16") { subView.removeConstraint(constraint) } } } }
HELP & SUPPORT