ios


Crash when accessing nested completion block — EXC_BAD_ACCESS


I've found a reliable crash with EXC_BAD_ACCESS while implementing a cache on my app. I've recreated the situation in a new project in about ~50 lines of code with 2 pods (Alamofire, HanekeSwift), and it crashes every time.
All I am doing is in my ViewController, calling a cache get:
CachingManager.sharedInstance.fetchAllThings({
result in
// result should be an array of objects
print(result)
})
and the CachingManager's fetchAllThings method is:
func fetchAllThings(completion: ([AnyObject] -> Void)?) {
let fetcher = CustomFetcher<JSON>(key: "HELLO")
fetcher.fetch(failure: {
error in
print(error)
}, success: {
json in
completion?(json.array)
})
}
Now, the CustomFetcher found above is implemented as such:
override func fetch(failure fail: ((NSError?) -> ()), success succeed: (JSON) -> ()) {
let endpoint = "www.google.com"
Alamofire.request(.GET, endpoint, parameters: nil, encoding: .URL, headers: nil)
.responseJSON {
(data) -> Void in
self.onReceiveData(data.data!, failure: fail, success: succeed)
}
}
private func onReceiveData(data: NSData, failure: ((NSError?) -> ()), success: (JSON) -> ()) {
success(JSON.Array(["HI"])) <--- CRASH HAPPENS HERE
}
I get an EXC_BAD_ACCESS when trying to run the success block in my onReceiveData.
Am I doing something wrong? Is this a Swift issue? Is this an issue with the pods I'm using?
For reference, linked is a project with the code above that will crash on launch every time if that helps anyone debug this.
Project on GitHub
Seems like the use of a Custom fetcher is not that clear in the Haneke docs. With the above fetchAllThings method, I was trying to fetch directly from the CustomFetcher, which was causing the bad access (that is not it's intended use, I am guessing). The correct way to use a fetcher with a JSON cache is:
func fetchAllThings(completion: ([AnyObject] -> Void)?) {
let fetcher = CustomFetcher<JSON>(key: "HELLO")
let cache = Shared.JSONCache
cache.fetch(fetcher: fetcher, formatName: "original", failure: {
error in
print(error)
}, success: {
json in
completion?(json.array)
})
}
The key here is that I am now using the library's cache object to invoke the Fetcher.
I hope this helps someone!

Related Links

Adding target to UIControl
Uploading multiple builds of same iOS Application
Swift json delete key
Delay start of animation for UIView with CAKeyframeAnimation?
Why does an NSObject have to be initialized before its' .class-property is readable
Offline mode for Google Maps iOS
What does glGenVertexArraysOES() do and where is the documentation?
dateFromComponents gives Wrong date
ios non-renewing subscription's duration in sandbox
Variable Cells in UITable
“.gif” File loads and then disappears after 1 second
Cannot close InAppBrowser on iPhone, done button is missing when page is opened.
How to delete a polyline in Mapbox iOS SDK?
Using NSDate to convert a string to a date, my dates are changed to 6 months back [duplicate]
Does anyone know what happened to the Parse.com documentation?
Sort array on string value (locale) in Realm - Swift

Categories

HOME
ssl
wix
apache-pig
oracle-adf
http2
drag
copy-paste
hyperion
cobol
git-svn
imp
ssrs-tablix
vast
oracle11gr2
collision
accelerated-mobile-page
broker
nice-language
jsgrid
spring-batch-admin
f2py
valueinjecter
reformatting
scatter-plot
stomp
groove
dashboard-designer
doctrine-odm
presto
disqus
response
bem
azure-management-api
syncfusion
koala-framework
mspgcc
dwr
biological-neural-network
javascript-security
jce
webloadui
android-touch-event
postmark
cabal-install
blocking
sqlite-net
surroundscm
ubuntu-15.04
printscreen
cglib
data-fitting
submission
namenode
illegalstateexception
parsoid
interpreted-language
linq2db
cng
cocoon-gem
avalonedit
ppl
react-rails
easynetq
emberfire
diagonal
mks
puppet-enterprise
cvs2git
esx
opensc
gdk
publish-actions
linguistics
youtrack-api
blazeds
showcaseview
execcommand
ccombobox
mms-gateway
jquery-data
google-authorship
python-dateutil
speaker
domain-mapping
file-structure
datakey
bapi
openid4java
settings.bundle
gemstone
blackberry-jde
jquery-1.3.2
maemo
resharper-4.5
stsadm
palm-pre

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App