39

I have a unique msgid for each ChatData object.

@interface ChatData : RLMObject
@property NSInteger msgid;
....
@end

But each time I create a new object I have to query all objects and get the last msgid.

RLMArray *all = [[ChatData allObjects] arraySortedByProperty:@"msgid" ascending:YES];
ChatData *last = [all lastObject];
ChatData *newData = [[ChataData alloc]init];
newData.msgid = last.msgid+1;

Is there an efficient way to replace this implementation?

0

6 Answers 6

58

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!).

11
  • 4
    Are there any plans to add autoincrement behavior to Realm? Nov 4, 2014 at 17:51
  • 1
    Yes, that functionality is coming, but I don't have a timeline for you.
    – jpsim
    Nov 5, 2014 at 17:04
  • 2
    It is okay to put this in the +defaultPropertyValues dictionary isn't it? I'm referring to [[NSUUID UUID] UUIDString].
    – Darrell
    Jan 16, 2015 at 1:46
  • 5
    For Swift use this NSUUID().UUIDString Dec 25, 2015 at 20:56
  • 1
    checkout github.com/realm/realm-cocoa/issues/2591#issuecomment-170259579 if anyone cares Realm autoincrement feature
    – Puttin
    Jan 16, 2016 at 2:34
16

You are use this Code for Auto Incremental Primary Key in Swift :

var myvalue = realm.objects(ChatData).map{$0.id}.maxElement() ?? 0
myvalue = myvalue + 1
3
  • 1
    App terminated with this error "Primary key can't be changed after an object is inserted". Any alternative way to update the Primary Key ? Aug 9, 2017 at 6:32
  • 1
    Great solution. Works like charm. Thank you :)
    – Hrishikesh
    Oct 4, 2017 at 10:32
  • 1
    func nextId() -> Int { return (realm?.objects(MyRealmObject.self).map{$0.id}.max() ?? 0) + 1 }
    – Elijah
    Nov 15, 2017 at 2:15
11

Autoincrement id Realm in Swift 2.0: insert code in class realm and object write use

import Foundation
import RealmSwift

class Roteiro: Object {

dynamic var id = 0
dynamic var Titulo = ""
dynamic var Observacao = ""
dynamic var status = false
dynamic var cadastrado_dt = NSDate()

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

//Incrementa ID
func IncrementaID() -> Int{
    let realm = try! Realm()
    if let retNext = realm.objects(Roteiro.self).sorted(byKeyPath: "id").first?.id {
        return retNext + 1
    }else{
        return 1
    }
}

in file write use:

let Roteiro_Add = Roteiro()
    //increment auto id
    Roteiro_Add.id = Roteiro_Add.IncrementaID()

    Roteiro_Add.Titulo = TituloDest
    Roteiro_Add.Observacao = Observacao
    Roteiro_Add.status = false


    let realm = try! Realm()
    try! realm.write({ () -> Void in
        realm.add([Roteiro_Add])
    })
3
  • 2
    Thanks bro :) Nice and Quick!
    – Atef
    Jun 15, 2016 at 13:15
  • Yeah, it might exists some issues on multi-thread write and read. which may need some additional processes.
    – Ryan Chou
    Aug 18, 2016 at 0:54
  • 2
    if let retNext = realm.objects(Roteiro.self).sorted(byKeyPath: "id").first?.id { this should be last item like if let retNext = realm.objects(Ordernew.self).sorted(byKeyPath: "id").last?.id { Apr 10, 2019 at 11:46
5

In Realm you need to manage auto-inc ID it self so there are many ways to manage it. Below is some of them.

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

call this method every time when you adding record.

1
  • 1
    I got this error "Primary key can't be changed after an object is inserted" @Tofaani Kaanudo Aug 9, 2017 at 6:26
1

I used a creationDate in my Model, so I created a Unix timeStamp based on this date, and used it as the primaryKey of my object.

It's 99.99% guaranteed to be unique in my case (because the timestamp is precise to the second), but it may depend on your use case. It's less robust than a UUID, but in many cases it's sufficient.

extension NSDate {

    /** Returns a NSDate instance from a time stamp */
    convenience init(timeStamp: Double) {
        self.init(timeIntervalSince1970: timeStamp)
    }
}


extension Double {

    /** Returns a timeStamp from a NSDate instance */
    static func timeStampFromDate(date: NSDate) -> Double {    
        return date.timeIntervalSince1970
    }
}
0

This is essentially what is suggested in jpsim's answer, using UUID to generate unique keys. We query prior to inserting to ensure uniqueness. This will most often only incur one query; in the very rare case of a collision it will continue until it finds a unique id. This solution is a natural extension on the Realm type and is generic over classes that inherits from Object. The class must implement primaryKey and return the name of a String property.

extension Realm {

    func createAutoUnique<T: Object>(_ type: T.Type) -> T {

        guard let primaryKey = T.primaryKey() else {

            fatalError("createAutoUnique requires that \(T.self) implements primaryKey()")
        }

        var id: String

        var existing: T? = nil

        repeat {

            id = UUID().uuidString

            existing = object(ofType: type, forPrimaryKey: id)

        } while (existing != nil)

        let value = [
            primaryKey: id
        ]

        return create(type, value: value, update: false)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.