I want to update an object with realm. But my app is crashing.
My code:
var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
shuf.unreadMessage=shuf.unreadMessage++
self.realm.beginWriteTransaction()
self.realm.commitWriteTransaction()
Crash Log:
Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
How can I fix ?
Try the following:
var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
self.realm.beginWriteTransaction()
shuf.unreadMessage++
self.realm.commitWriteTransaction()
The issue, as the exception message said, was that you were modifying the object outside of the write transaction. Doing that mutation inside the transaction should do the trick!
Related
I am trying to start a one on one call using Azure CommunicationCalling sdk. I am getting a crash inside one of the sdk functions as shown in screenshot for backtrace below.
Code for start call:-
func startCall(callee: String, successHandler: #escaping (Call?) -> Void ) {
// start call logic
debugPrint(callee)
let callees:[CommunicationIdentifier] = [createCommunicationIdentifier(fromRawId: callee)]
debugPrint(callees)
self.callAgent?.startCall(participants: callees, options: StartCallOptions()) { (call, error) in
if let error = error {
debugPrint(error.localizedDescription)
successHandler(nil)
} else {
self.call = call
debugPrint("call placed successfully")
successHandler(call)
}
}
}
I have verified that call agent and communication identifier arr obj passed is not nil.
Crash log:-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
terminating with uncaught exception of type NSException
BackTrace for crash:-
I have referred to following documentation:-
https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/ui-library/get-started-composites?tabs=kotlin&pivots=platform-ios
This crash doesnt happen anymore with same sdk version. Also the beta sdk version (v2.3.0-beta.2) is working fine too.
NSKeyedUnarchiver.UnarchivedArrayOfObjects() terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]
track conforms to NSManagedObject
However decodedTrackLocation is declared #Published public var decodedTrackLocation: [CLLocation] = [], as it is not persistent, but supposed to be in memory.
The below code works on certain 'saves', but sometimes it throws an exception and I can't figure out what to do about it.
It crashes at 'if let final =' and throws this exception:
2021-06-01 17:42:28.052628+0200 GeoTrack[12300:1124356] -[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50
2021-06-01 17:42:28.061055+0200 GeoTrack[12300:1124356] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
*** First throw call stack:
(0x1a9e27298 0x1bdb81480 0x1a9d362a8 0x1a9e298f4 0x1a9e2b89c 0x1ad67168c 0x1ad707e24 0x1026e5988 0x102704310 0x1b0345f94 0x1b0345fb0 0x1b0345f94 0x1b0737d74 0x1b0733e68 0x1b02225f4 0x1b088b190 0x1031f7ae8 0x102ee7ae8 0x102ee932c 0x102ef776c 0x1a9da62e0 0x1a9da0740 0x1a9d9f818 0x1c04a5570 0x1ac6cb0e8 0x1ac6d0664 0x1025696dc 0x1a9a7e140)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
terminating with uncaught exception of type NSException
The reason the NSKeyedUnarchiver fails is because I did something stupid, I stored the wrong Data. What I can't grasp is why the do-try-catch block this code lives in, doesn't work/catch.
Any help would be appreciated.
The code in Question:
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
let archiver = try NSKeyedUnarchiver(forReadingFrom: out)
archiver.requiresSecureCoding = true
archiver.decodingFailurePolicy = .setErrorAndReturn
if let final = archiver.decodeArrayOfObjects(ofClass: CLLocation.self, forKey: NSKeyedArchiveRootObjectKey){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
if let final = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: CLLocation.self, from: out){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
Because it's not an Error that is thrown (throws), it's a NSInvalidArgumentException, a NSException, on Objective-C one. You can look for it :) - Larme
I learned something new!
Adding this line to my App import AlgoliaSearch returns this error:
2017-05-08 22:35:07.814593 XXXXXX[6255:2627487] -[__NSDictionaryI
length]: unrecognized selector sent to instance 0x1702f0980 2017-05-08
22:35:07.816565 XXXXXX[6255:2627487] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSDictionaryI length]: unrecognized selector sent to instance
0x1702f0980'
* First throw call stack: (0x192f351b8 0x19196c55c 0x192f3c268 0x192f39270 0x192e3280c 0x1015c8608 0x10158d72c 0x10194a198
0x1001f6c24 0x1001f8c9c 0x100091fd4 0x1003973d4 0x100397690
0x1003be2c0 0x101a5d258 0x101a5d218 0x101a62280 0x192ee2810
0x192ee03fc 0x192e0e2b8 0x1948c2198 0x198e557fc 0x198e50534
0x10013c734 0x191df15b8) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
The line of code it is referring to is as below:
self.ref.child("users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
for user in snapshot.children{
var numVids = NSDictionary()
if ((user.hasChild("videos")) == true){
--> numVids = user.value["videos"] as! NSDictionary
}
}
As soon as I remove the import Algolia module the error is removed, I have tried changing the version of the module and even switching to use the objc version but the error was the same.
Anyone seen something like this before?
https://github.com/algolia/algoliasearch-client-swift
https://github.com/algolia/algoliasearch-client-objc
I have created a subscription for insert & it sends push notification as follows
let subscriptionNotification = CKNotificationInfo()
subscriptionNotification.alertBody = "New Book has been created by Author"
subscriptionNotification.shouldBadge = true
subscription.notificationInfo = subscriptionNotification
When I receive the notification I open the app & I am simply trying to reset badge to zero
let resetBadge = CKModifyBadgeOperation(badgeValue: 0)
resetBadge.modifyBadgeCompletionBlock = {(error) in
if error != nil {
print(error)
}
else {
application.applicationIconBadgeNumber = 0
}
}
CKContainer.defaultContainer().addOperation(resetBadge)
I run this code on appdelegate did finish launching with options method.
I get following error. Keep in mind the name of the project is ckreference demo.
2015-12-15 21:41:58.417 ckreference demo[2486:1478837] -[CKNotification queryNotificationReason]: unrecognized selector sent to instance 0x15cd8a960
2015-12-15 21:41:58.427 ckreference demo[2486:1478837] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CKNotification queryNotificationReason]: unrecognized selector sent to instance 0x15cd8a960'
* First throw call stack:
(0x183521900 0x182b8ff80 0x18352861c 0x1835255b8 0x18342968c 0x1000b8b9c 0x1000b9250 0x1884cbae4 0x1884b9de0 0x184ad7790 0x184ad7b10 0x1834d8efc 0x1834d8990 0x1834d6690 0x183405680 0x184914088 0x18827cd90 0x1000b94d0 0x182fa68b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any suggestions on how to stop app from crashing?
The crash happens here
I've been looking at similar questions, but I still don't understand where I'm going wrong in my situation. Getting this error:
MyAwesomeApp[3481:1330310] -[Session setClient:]: unrecognized selector sent to instance 0x16e05de0
2015-03-15 07:30:29.990 MyAwesomeApp[3481:1330310] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Session setClient:]: unrecognized selector sent to instance 0x16e05de0'
*** First throw call stack:
(0x28e1045f 0x36ca6c8b 0x28e15879 0x28e13797 0x28d45008 0x646c4 0x628b4 0x62ba4 0x2c35de7f 0x2c4c1b35 0x2c35de7f 0x2c35de21 0x2c348c43 0x2c35d88d 0x2c35d567 0x2c356e31 0x2c32d759 0x2c5a12f9 0x2c32c1a9 0x28dd6fbf 0x28dd63cf 0x28dd4a35 0x28d223b1 0x28d221c3 0x3034f201 0x2c38c43d 0xa7a24 0xa7a60 0x37232aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
When I execute this code:
#IBAction func saveButton(sender: AnyObject) {
if session != nil {
editSession()
} else {
createSession()
}
navigationController?.popViewControllerAnimated(true)
}
func editSession() {
session?.sessionTitle = sessionTitleTxt.text
managedObjectContext?.save(nil)
}
func createSession() {
let entityDescription = NSEntityDescription.entityForName("Session", inManagedObjectContext: managedObjectContext!)
let newSession = Session(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
if sessionTitleTxt.text == "" {
newSession.sessionTitle = "Untitled Session"
} else {
newSession.sessionTitle = sessionTitleTxt.text
newSession.client = self.client!
}
managedObjectContext?.save(nil)
SessionsTVC?.client = newSession.client
}
Session class is:
import Foundation
import CoreData
#objc(Session)
class Session: NSManagedObject {
#NSManaged var sessionDate: String
#NSManaged var sessionTitle: String
#NSManaged var sessionNotes: String
#NSManaged var sessionExercises: NSSet
#NSManaged var client: Client
}
Can anyone help me out? I was getting a nil for the client variable before because I have several segues that I had confused, but now I'm getting this selector error and not sure how to move forward.
Thanks for any suggestions!
The error you are getting,
MyAwesomeApp[3481:1330310] -[Session setClient:]: unrecognized selector sent to instance 0x16e05de0
2015-03-15 07:30:29.990 MyAwesomeApp[3481:1330310] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Session setClient:]: unrecognized selector sent to instance 0x16e05de0'
happens when a method that is to be executed cannot be found. This can be thrown either because a method does not exist, or it is being called on the wrong object. In your specific case, the method setClientis being called on the Session object, which is being stored at the memory address of 0x16e05de0
Make sure that the setClient method exists and is supposed to be called on that Session object.
Here is a good link to help you with debugging:
http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
Hope this helps!!