Grumme Grumme - 2 months ago 156
iOS Question

Auto increment ID in Realm, Swift 3.0

After a lot of troubles, i finally got my code converted to Swift 3.0.

But it seems like my incrementID function isn't working anymore?

Any suggestions how i can fix this?

My incrementID and primaryKey function as they look right now.

override static func primaryKey() -> String? {
return "id"
}

func incrementID() -> Int{
let realm = try! Realm()
let RetNext: NSArray = Array(realm.objects(Exercise.self).sorted(byProperty: "id")) as NSArray
let last = RetNext.lastObject
if RetNext.count > 0 {
let valor = (last as AnyObject).value(forKey: "id") as? Int
return valor! + 1
} else {
return 1
}
}


The error i get:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_SwiftValue 0x60800009dd80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** First throw call stack:
(
0 CoreFoundation 0x00000001013af34b exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000100e1021e objc_exception_throw + 48
2 CoreFoundation 0x00000001013af299 -[NSException raise] + 9
3 Foundation 0x00000001009ec035 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
4 Foundation 0x0000000100917f2b -[NSObject(NSKeyValueCoding) valueForKey:] + 283
5 Fitness Friend 0x00000001000ee543 _TFC14Fitness_Friend11TmpExercise11incrementIDfT_Si + 995
6 Fitness Friend 0x0000000100058a23 _TFC14Fitness_Friend18ExerciseManagerDAO14getOneExercisefTSS4toolSS_CS_11TmpExercise + 867
7 Fitness Friend 0x0000000100082a74 _TFC14Fitness_Friend17WorkoutManagerDAO23beginnerFullBodyWorkoutfT_T_ + 868
8 Fitness Friend 0x000000010007e6a8 _TFC14Fitness_Friend17WorkoutManagerDAO8initDatafT_T_ + 152
9 Fitness Friend 0x000000010007e5bf _TFC14Fitness_Friend17WorkoutManagerDAOcfT_S0_ + 207
10 Fitness Friend 0x000000010007e46c _TFC14Fitness_Friend17WorkoutManagerDAOCfT_S0_ + 44
11 Fitness Friend 0x000000010007e431 globalinit_33_DC3CCE82FCFD82D905A1530A0DB36957_func0 + 17
12 libdispatch.dylib 0x00000001052630cd _dispatch_client_callout + 8
13 libdispatch.dylib 0x00000001052481fc dispatch_once_f + 501
14 Fitness Friend 0x000000010007e4e4 _TFC14Fitness_Friend17WorkoutManagerDAOau14sharedInstanceS0_ + 36
15 Fitness Friend 0x00000001000bc051 _TFC14Fitness_Friend14WorkoutManager16getCustomWorkoutfT_CS_13CustomWorkout + 17
16 Fitness Friend 0x000000010008caac _TFC14Fitness_Friend26TableWorkoutViewController14viewWillAppearfSbT_ + 780
17 Fitness Friend 0x000000010008cd81 _TToFC14Fitness_Friend26TableWorkoutViewController14viewWillAppearfSbT_ + 49
18 UIKit 0x0000000102237de3 -[UIViewController _setViewAppearState:isAnimating:] + 692
19 UIKit 0x00000001022384f3 -[UIViewController __viewWillAppear:] + 147
20 UIKit 0x0000000102273178 -[UINavigationController _startTransition:fromViewController:toViewController:] + 847
21 UIKit 0x00000001022740b9 -[UINavigationController _startDeferredTransitionIfNeeded:] + 874
22 UIKit 0x000000010227519b -[UINavigationController __viewWillLayoutSubviews] + 58
23 UIKit 0x000000010246c1b7 -[UILayoutContainerView layoutSubviews] + 223
24 UIKit 0x0000000102155344 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
25 QuartzCore 0x000000010896ecdc -[CALayer layoutSublayers] + 146
26 QuartzCore 0x00000001089627a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
27 QuartzCore 0x000000010896261e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
28 QuartzCore 0x00000001088f062c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
29 QuartzCore 0x000000010891d713 _ZN2CA11Transaction6commitEv + 475
30 QuartzCore 0x000000010891e083 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
31 CoreFoundation 0x0000000101353e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23
32 CoreFoundation 0x0000000101353d87 __CFRunLoopDoObservers + 391
33 CoreFoundation 0x0000000101338b9e __CFRunLoopRun + 1198
34 CoreFoundation 0x0000000101338494 CFRunLoopRunSpecific + 420
35 GraphicsServices 0x0000000106eb0a6f GSEventRunModal + 161
36 UIKit 0x0000000102090f34 UIApplicationMain + 159
37 Fitness Friend 0x000000010006aaef main + 111
38 libdyld.dylib 0x00000001052af68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Answer

There's no need to use KVC here, or to create a sorted array just to get the max value. You can just do:

func incrementID() -> Int {
    let realm = try! Realm()
    return (realm.objects(Exercise.self).max(ofProperty: "id") as Int? ?? 0) + 1
}
Comments