ios - 我正在尝试更新 GCD 中的 Realm 对象,但是当我的代码运行时,它会因错误而崩溃,如下所示

标签 ios swift realm

*** Terminating app due to uncaught exception 'RLMException', reason: 'Object is already persisted in a Realm'
*** First throw call stack:
(
0   CoreFoundation     0x000000010aa58e65 __exceptionPreprocess + 165
1   libobjc.A.dylib    0x000000010c873deb objc_exception_throw + 48
2   Realm              0x000000010948c22e RLMAddObjectToRealm + 2370
3   RealmSwift         0x0000000109878321 _TFC10RealmSwift5Realm3addfS0_FTCS_6Object6updateSb_T_ + 1553
4   vehiclelogbookgps  0x0000000108af88db _TFFFFC17vehiclelogbookgps11AppDelegate11backServiceFS0_FT_T_U_FT_T_U_FT_T_U_FT_T_ + 443
5   Realm              0x00000001094e4cf7 -[RLMRealm transactionWithBlock:error:] + 54
6   RealmSwift         0x0000000109877bc8 _TFC10RealmSwift5Realm5writefS0_FzFT_T_T_ + 136
7   vehiclelogbookgps  0x0000000108af86bd _TFFFC17vehiclelogbookgps11AppDelegate11backServiceFS0_FT_T_U_FT_T_U_FT_T_ + 333
8   libdispatch.dylib  0x000000010d43ee5d _dispatch_call_block_and_release + 12
9   libdispatch.dylib  0x000000010d45f49b _dispatch_client_callout + 8
10  libdispatch.dylib  0x000000010d4472af _dispatch_main_queue_callback_4CF + 1738
11  CoreFoundation     0x000000010a9b8d09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
12  CoreFoundation     0x000000010a97a2c9 __CFRunLoopRun + 2073
13  CoreFoundation     0x000000010a979828 CFRunLoopRunSpecific + 488
14  GraphicsServices   0x0000000111313ad2 GSEventRunModal + 161
15  UIKit              0x000000010b350610 UIApplicationMain + 171
16  vehiclelogbookgps  0x0000000108af6ab5 main + 117
17  libdyld.dylib      0x000000010d49392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码是:

internal func backService(){


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in

        var anObject = try! Realm().objects(remindertbl)
            print(anObject.count)
            for loop in anObject
            {


                var dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "dd/MM/yyyy hh:mm a"
                var  timestamp = dateFormatter.stringFromDate(NSDate())
                var timeStampint = dateFormatter.dateFromString(timestamp)!.timeIntervalSince1970
                if(loop.reminderdays == Int64(timeStampint))
                {
                    if(loop.status == 0){
                    let notification = UILocalNotification()
                    notification.fireDate = NSDate(timeIntervalSince1970: 0)
                    notification.alertBody = "\(loop.title)"
                    notification.alertAction = "swipe to opem VLB Cloud!"
                    notification.soundName = UILocalNotificationDefaultSoundName
                    notification.userInfo = ["CustomField1": "w00t"]
                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                        loop.status = 1
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            // Update UI
                             try! Realm().write()
                            {
                            let updateStAatus = loop
                                do
                                {
                                    var input = try Realm().add(updateStAatus,update: true)
                                    print(input)
                                }
                                catch
                                {
                                    print("error")
                                }
                            }

                        })


                    }
                }
            }




        })


}

最佳答案

请清理您的元素。

       if(loop.reminderdays == Int64(timeStampint))
            {
                if(loop.status == 0){


                    let notification = UILocalNotification()
                    notification.fireDate = NSDate(timeIntervalSince1970: 0)
                    notification.alertBody = "\(loop.title)"
                    notification.alertAction = "swipe to opem VLB Cloud!"
                    notification.soundName = UILocalNotificationDefaultSoundName
                    notification.userInfo = ["CustomField1": "w00t"]
                    UIApplication.sharedApplication().scheduleLocalNotification(notification)

                    let updateObject = try! Realm().objects(remindertbl).filter("status = 0")
                    let reminderObject = remindertbl()
                    for updateStatus in updateObject{
                        try! Realm().write()
                            {
                                reminderObject.status = 1
                                reminderObject.reminderId = updateStatus.reminderId
                                try! Realm().add(reminderObject, update: true)
                        }
                        break // You can remove this if you have more record on same time
                    }
                    break
                }
            }

关于ios - 我正在尝试更新 GCD 中的 Realm 对象,但是当我的代码运行时,它会因错误而崩溃,如下所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993449/

相关文章:

ios - 如何在 Swift 3.1 后台使用调度队列实现高优先级

iphone - 创建类似 PassCode 功能的 UI(引用 dropbox 应用程序)

java - 来自 Intent 服务的 Realm 同步写入

ios - 删除结果类型数组( Realm 类型)的元素

swift - 如何在这个 Swift for-in 示例中打印两个键值对

android - Realm SyncConfiguration.Builder 已弃用

iphone - 暂停 CCAction cocos2d

ios - 无法滚动到 UITableView 的底部

ios - 当我尝试为 CIContext 设置选项时,类型 'String' 没有成员 'cacheIntermediates' 错误

ios - DispatchQueue - QOS 困惑