Realm doesn't have auto increment behavior, so you'll need to manage that yourself. A question I'd encourage you to ask yourself about your data:
Is it necessary to have sequential, contiguous, integer ID's?
If not, then a unique string primary key might be sufficient. Then you can use something like [[NSUUID UUID] UUIDString]
to generate unique string ID's. The nice thing about this is that these UUID's are more or less guaranteed to be unique, even in multithreaded scenarios.
If so, it might be more efficient to always keep the last number in memory, so that queries aren't required every time a new ID should be generated. If objects might be created in multiple threads, make sure to make your nextPrimaryKey()
function thread-safe, otherwise it might generate the same number twice (or more!).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…