-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash after Swift 3 update #56
Comments
Thanks, I'll check this out today |
Do you have some news? |
Hey @andrealufino, I don't see any specific problems with this line in swift 3. In fact, it should be safer than previously since we wrap those try blocks to catch errors now. Does this crash on a device as well as the sim? |
Yes, it crashes on both. |
Hi @andrealufino |
I tried, but in the example app everything is working good.
News is a struct which implements the Storable protocol. Could be that if the array is empty (or nil) something happens? I don't know, but I can assure you this was working really good with the older version (before Swift 3). |
Empty arrays are packed correctly, and from the code you posted the array can't be nil (unless you use force unwrapping, but in that case it would bail out earlier) |
Exactly, that array will never be nil. It comes from the server, but even if I have no news to show, I pass an empty array and not a nil one. The weird thing is that |
What if some attribute of the a |
Yeah, that might be the issue as far as I can tell |
This thing has changed from previous version, right? |
Yep, this is due to the JSON serialization. |
Ok, I was able to reproduce the issue. Let me see if I can fix it. |
The problem is inherent to Swift 3's let packet: [String: Int?] = ["one": nil]
let dictionary: [String: Any] = ["packet": packet]
do {
let data = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
} catch {
print("error")
} I'm not sure if reverting to the use of In the meantime @andrealufino you can use nil coalescing in your struct News: Storable {
var title = ""
var body: String? = nil
init?(warehouse: Warehouseable) {}
func toDictionary() -> [String: Any] {
return ["title": title, "body": body]
}
} |
In my structures there is no toDictionary() method implementation however and I've never needed it. Could this be another problem? I can assure you everything was working good even without it. This is my News struct :
ps: in my opinion is better to keep the Any instead of AnyObject for flexibility reasons adding maybe an assert to make people understand what is causing the problem. |
@andrealufino check out branch |
Will do! |
Thanks for the detailed investigation @andreamazz. I agree that for safety we should check for the ability to create JSON before we actually try it, particularly since it'll crash here and not throw an error (why even both having it throw if you're not going to handle cases like this, Apple?). If we can, we should work around these issues as well. We should be able to serialize / deserialize an |
Hey @nickoneill After some digging it looks like that the issue happens when naively implementing func toDictionary() -> [String: Any] {
return ["name": name, "age": age]
} This fails. If we use the default implementation provided by |
I thought @andrealufino didn't provide a |
@nickoneill exactly, I didn't provide it. I still have to check the new branch, hope I can do it tomorrow. |
Can you post a print of your object's toDictionary call? |
@andrealufino I'm guessing the new branch will prevent the crash, but it also won't pack your News object. Can you print a detailed list of everything in the News object that crashes the JSONSerializer? |
This is the News struct :
The attribute that is always nil (for now) is the |
How about a description of the actual object that is failing to serialize? I didn't even notice all the IUOs before but I'm guessing they're the problem. It doesn't look like they're necessary for your object either - do you know why you're using them? |
Using the
somewhere in the code. I'm printing the parameter The isUserFirstAccess is declared in this way into a class :
Every time something in that class changes, I pack again the whole object, that's why I wrote the self there. ps : I can't print the whole object because contains sensitive information (it's not only the News object which crashes, now is this). Do you think could be the Basically, Pantry is an essential part of this project and I hope we can make it works :) |
Do you have a solution? Even temporary, even If I have to change something to make it works with the current Pantry version |
Let's clear up some confusion. Did you test the |
Hey guys, sorry for the late response. I tried that branch, but the same happened : |
I'll bring up the same issue as before. Is there a reason for the IUOs that I'm missing? They look unnecessary. You're going to crash on accessing whatever property is an IUO if you try to access it normally as well. The only difference here is that Pantry is trying to access each of your properties in an attempt to store them so the issue is automatically discovered. @andreamazz I'm still a fan of checking our valid JSON before we write it. Can you submit a PR from your branch? |
You're right, however, no, no specific reason, so they can be removed. I think this should be however handled somewhere, because in the old Pantry version everything was good. |
Hey guys :) Do you have some news about this? |
Accidentally saw this issue when I was searching how to solve my similar problem. Eventually I decided it with remove all optional values in serialized dictionary. Maybe it helps. |
@AntiPavel Specifically IUOs and not just optionals, right? |
Yes, right |
@AntiPavel @andrealufino can you see if the latest master (not a versioned release on cocoapods yet) is a reasonable fix for you? |
Hi guys, I've just checked the latest master, but always the same. It crashes at line 117, here :
I think it's something added in the last commit. The error is
|
In my case, I had one nested class. Let's say
The type of the mandatory reference is serialized by After that, the problem was gone and my object of type Hope this help can help you. |
@andrealufino I've solve it. I think maybe helpful for you. in class AA when I do .. same as your project |
Hello guys, I'm having a serious problem with Pantry. I didn't change anything from what I had before the update (it's a project that could wait). After this update, Pantry crashes at every
pack
call. I pack arrays (of every type, even custom structs that conforms to theStorable
protocol) and other kind of stuff. Before the update everything was working good, not now.One of the call is simple as :
Pantry.pack(self, key: pantryKeyCurrentUser)
Here crashes pointing me to this line (number 118 of file JSONWarehouse):
let data = try JSONSerialization.data(withJSONObject: storableDictionary, options: .prettyPrinted)
The error is
EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0)
The text was updated successfully, but these errors were encountered: