I'm accessing a variable in one of my functions that got passed from a few other functions, but when it reaches the line where I use it in this final function, I get a EXC_ACCESS_ERROR
, however I can inspect the value in the debugger and confirm it has a value.
From the top, I'll show the stack of function call:
First, inside my ViewController, there is a CoreData
Model variable
var expense : Expense?
*it gets assigned a value in the previous viewController
Then, in the same ViewController, I call the first function:
TimekeeperClient.sharedInstance().getExpenseAttachments(expenseID: expense.tkID) { (success, statusCode) in
//Do some work
}
Inside getExpenseAttachments
, the code is as follows and I call the next function:
func getExpenseAttachments(expenseID: Int64,completion completionHandlerForAttachments: @escaping (_ success: Bool, _ statusCode: Int?) -> Void)
{
let url = Methods.getExpenseAttachmentsListUrl(expenseID: Int(expenseID))
Alamofire.request(url, headers: getHeaders()).responseArray { (response : DataResponse<[AttachmentResponse]>) in
guard let attachmentsResponse = response.result.value else
{
print("Could not map attachments from server response")
completionHandlerForAttachments(response.result.isSuccess, response.response?.statusCode)
return
}
let attachmentsToSync = AttachmentRepository.getUnsyncedExpenseAttachments(expenseID: expenseID,tkExpenseAttachmentList: attachmentsResponse)
//Do some other stuff
}
}
}
IMPORTANT: note that the expenseID
value passed in from the previous function does have a value when the line let url = Methods.getExpenseAttachmentsListUrl(expenseID: Int(expenseID))
is executed.
Then, the next the final function call (and where the problem is) is AttachmentRepository.getUnsyncedExpenseAttachments(...
. This function has the following body:
static func getUnsyncedExpenseAttachments(expenseID: Int64, tkExpenseAttachmentList: [AttachmentResponse]) -> [AttachmentResponse] {
var attachmentResponse:[AttachmentResponse] = [AttachmentResponse]()
//Get Core Data Attachment List
let attachmentRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Attachment")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)
let predicate = NSPredicate(format: "expense.tkID == %@", expenseID)
attachmentRequest.sortDescriptors = [sortDescriptor]
attachmentRequest.predicate = predicate
//Do some CoreData Stuff
}
The app crashes with the EXC_ACCESS_ERROR
on the line let predicate = NSPredicate(format: "expense.tkID == %@", expenseID)
when trying to access the expenseID variable. What is strange is if I hover over the variable, I can see xcode showing the expected value of "15543"
.
What am I missing?