*** 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")
}
}
})
}
}
}
})
}
答案 0 :(得分:2)
请清楚你的物品。
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
}
}
答案 1 :(得分:1)
这是导致崩溃的行
loop.status = 1
因为你没有把它放在writ() {}