-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infinite loop in db query [Realm Swift 2.6.2] #4895
Comments
Hey @shuhaodo. Thanks for reaching out. Someone will follow-up soon. |
@shuhaodo please share the following:
|
EdoContact definition: open class EdoContact: Object{
dynamic var displayName = ""
dynamic var firstName = ""
dynamic var lastName = ""
dynamic var lastUpdatedTime = Date.distantPast
dynamic var contactedCount = 0
dynamic var lastContactTime = Date.distantPast
dynamic var note = ""
fileprivate var _items = List<EdoContactItem>()
override open static func primaryKey() -> String {
return "displayName"
}
override open static func indexedProperties() -> [String] {
return ["firstName", "lastName"]
}
open func items()->Results<EdoContactItem>{
return _items.filter(NSPredicate(value: true))
}
open func addItem(_ item:EdoContactItem){
self._items.append(item)
}
open func deleteItem(_ index:Int){
if index < 0 {
self._items.removeAll()
}else{
self._items.remove(at: index)
}
}
} The value of "displayName" is leave-fd8115791a3c402029-fe6116757c6406757114-ff2b11797461-fe871372726d067a73-fef715737c670c. |
@youbing @shuhaodo here's my best attempt at piecing together a full app based on the code fragments you provided that attempts to reproduce the issue. Attempted Repro Case AppDelegate.swiftimport UIKit
import RealmSwift
open class EdoContact: Object{
dynamic var displayName = ""
dynamic var firstName = ""
dynamic var lastName = ""
dynamic var lastUpdatedTime = Date.distantPast
dynamic var contactedCount = 0
dynamic var lastContactTime = Date.distantPast
dynamic var note = ""
// Commented out because no definition for 'EdoContactItem' was provided
// fileprivate var _items = List<EdoContactItem>()
override open static func primaryKey() -> String {
return "displayName"
}
override open static func indexedProperties() -> [String] {
return ["firstName", "lastName"]
}
// Commented out because no definition for 'EdoContactItem' was provided
// open func items()->Results<EdoContactItem>{
// return _items.filter(NSPredicate(value: true))
// }
//
// open func addItem(_ item:EdoContactItem){
// self._items.append(item)
// }
//
// open func deleteItem(_ index:Int){
// if index < 0 {
// self._items.removeAll()
// }else{
// self._items.remove(at: index)
// }
// }
}
// Best guess as to how the 'getContactByDisplayName' function provided was implemented
extension Results where Element == EdoContact {
func getContactByDisplayName(_ displayName:String?) -> EdoContact? {
if let id = displayName {
return filter("displayName = [c]%@", id).first //case insensitive
} else {
return nil
}
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Potentially clean up from previous runs
_ = try? FileManager.default.removeItem(at: Realm.Configuration.defaultConfiguration.fileURL!)
let value = "leave-fd8115791a3c402029-fe6116757c6406757114-ff2b11797461-fe871372726d067a73-fef715737c670c"
let realm = try! Realm()
try! realm.write {
let contact = EdoContact()
contact.displayName = value
realm.add(contact)
}
if let contact = realm.objects(EdoContact.self).getContactByDisplayName(value) {
// Successfully prints the 'EdoContact' object that was added
print(contact)
}
return true
}
} As you can see, I can't reproduce the issue you're seeing with Realm Swift 2.6.2. |
Please test it on device. This issue doesn't happen on simulator. When the app is running, break the app, the call stack can be:
|
@shuhaodo @youbing I also tried to run JP's code with the device, but the crash didn't reproduce. It is very hard to reproduce the issue with the information you shared. Could you please send us a reproducible sample code or an entire project? If you cannot publish your code, please send it to |
Can you please insert some items into EdoContact? At least, there should be an item whose displayName value is "leave-fd8115791a3c402029-fe6116757c6406757114-ff2b11797461-fe871372726d067a73-fef715737c670c". Then you can search the item which has the displayName value - "leave-fd8115791a3c402029-fe6116757c6406757114-ff2b11797461-fe871372726d067a73-fef715737c670c." |
…operty has long string that contains number Original issue is here #4895 If objects has primary key property, then the property has "1111111111111111111111111111111" and "1111111111111111111111111111112", then case-insensitive search fails (or taking very long time?) if search query is "name == [c]%@", "1111111111111111111111111111111" If the property isn't primary key, the query succeeds. If the property doesn't have number like "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", the query succeeds. If the property is a little bit shorter, the query succeeds after a long time.
@youbing Thank you very much. I can reproduce the issue. let value = "1111111111111111111111111111111"
let realm = try! Realm()
try! realm.write {
let obj1 = TestObj()
obj1.name = value
realm.add(obj1)
let obj2 = TestObj()
obj2.name = "1111111111111111111111111111112"
realm.add(obj2)
}
if let obj = realm.objects(TestObj.self).filter("name = [c]%@", "1111111111111111111111111111111").first {
print(obj)
}
class TestObj: Object {
dynamic var name = ""
override static func primaryKey() -> String? {
return "name"
}
} |
Presumably a core issue due to @danielpovlsen's recent work? |
A small reproducible smaple code is above. Also failing test case is here 68bb564 If objects has primary key property, then the property has "1111111111111111111111111111111" and "1111111111111111111111111111112",
Maybe the query takes so long time. It depends on what the property value is. The backtrace while querying is the following.
|
I filed an issue against core with a core test case and some further analysis. |
@kishikawakatsumi Ok. Thanks. Looking forward to the new build. |
In the latest Realm update, when running a filter query involves case-insensitive string compare, it went into an infinite loop causing the phone heats up.
Steps to Reproduce
This is 100% reproducible with this query:
("displayName = [c]%@", id)
It worked fine till the 2.6.1 update, and still happens in 2.6.2.
Code Sample
Here is the code snippet:
Version of Realm and Tooling
The text was updated successfully, but these errors were encountered: