Skip to content
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

There is a problem using the filtering function. #7643

Closed
sozohoy opened this issue Jan 28, 2022 · 18 comments
Closed

There is a problem using the filtering function. #7643

sozohoy opened this issue Jan 28, 2022 · 18 comments
Labels
O-Community Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@sozohoy
Copy link

sozohoy commented Jan 28, 2022

First of all, I'm Korean, so please understand that I can use wrong English.

The function I want to do is the process of filtering the lecture data in the database, and I want to get the total number of data in the database.

However, I tried to proceed as in the official documentation, but it didn't work.
What should I do?
The code below is the code to load data.

func readData(){

    let courseDB = realm.objects(testCourseData.self)
    
    let searchText = 1
    let testDB = realm.objects(testCourseData.self).filter("num == \(searchText)")
    print(testDB.count)
    var readCN = String()
    var readRN = String()
    var readPR = String()
    var readMJ = String()
    var readCF = String()
    var readNM = Int()
    for i in 0...2003{
        readCN = courseDB[i].courseName
        readRN = courseDB[i].roomName
        readPR = courseDB[i].professor
        readMJ = courseDB[i].major
        readCF = courseDB[i].classification
        readNM = courseDB[i].num
        courseName.append(readCN)
        roomName.append(readRN)
        professor.append(readPR)
        major.append(readMJ)
        classification.append(readCF)
        num.append(readNM)
        tableView?.reloadData()
        
    }
}

스크린샷 2022-01-28 오후 11 46 18

The code below is an error code. What should I do? I hope you take into account that I am a beginner. thank you.

@sozohoy
Copy link
Author

sozohoy commented Jan 28, 2022

let searchText = 1
let testDB = realm.objects(testCourseData.self).filter("num == \(searchText)")
print(testDB.count)

When the above code is deleted, it runs normally! Only that code has a problem.

@pavel-ship-it
Copy link
Contributor

HI @sozohoy
What version of Realm you're running?
Can you show testCourseData model?
How you opening the Realm?
How do you call readData()?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Jan 31, 2022
@sozohoy
Copy link
Author

sozohoy commented Feb 4, 2022

I've made some changes to the database structure, but I'm still having the same problem, so I'll answer the current Database.
My RealmSwift version is 10.20.
스크린샷 2022-02-04 오후 11 38 34
스크린샷 2022-02-04 오후 11 39 14
model like this.. and
I open realm using the code below.
let realm = try! Realm()
And the current function I want is to return the number of data in the DB when the button is pressed, and the code is as follows.

@IBAction func addBtnClicked(_ sender: Any) {
    let readMyDB = realm.objects(userDB.self)
    var myDbCnt = readMyDB.count
}

Finally, the same error (EXC_BAD_ACCESS) occurs when I try to run other functions (filtering, sorting) etc. Answers I'll wait. thank you!

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Feb 4, 2022
@jsflax
Copy link
Contributor

jsflax commented Feb 4, 2022

@sozohoy do you have custom functions for sorting as well? Your filter function looks like it might be calling itself, which could be a problem.

@sozohoy
Copy link
Author

sozohoy commented Feb 4, 2022

There is currently no sorting functions. I didn't write it separately because an error occurred.

@sozohoy
Copy link
Author

sozohoy commented Feb 4, 2022

What I am curious about is that if I call readMyDb[0].example
as above, I can know the value, but I do not know what to do when I do not know how many data are stored in the DB.
If you want to see my project code.. please check my github
https://github.com/uswLectureEvaluation/IOS

@sozohoy
Copy link
Author

sozohoy commented Feb 4, 2022

I tested it in another project and it works there. There seems to be a problem with a project I'm working on.
So I guess I should give a general description of the project I'm working on.
I am making a school timetable app.
First, save the data stored in the external Firebase to the realm database. After that, the timetable year, semester, and name created by the user are saved in realm.
After that, the first problem occurs while loading timetables into timetableview. The above error occurred when getting the number of lecture data stored in realm. So when I first save firebase data in realm, I actually counted the number and solved it to some extent by retrieving the value. However, there is a problem when retrieving the number of data directly saved by the user. I really want to know which part is the problem. :(

@pavel-ship-it
Copy link
Contributor

@sozohoy ok, first of all you are adding Realm via Podfile and Swift Package Manager at once. Please stick to a single source.
Could you share the call stack with the exception message?

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Feb 4, 2022
@sozohoy
Copy link
Author

sozohoy commented Feb 5, 2022

@pavel-ship-it I didn't understand the first thing you said.

스크린샷 2022-02-05 오후 8 57 49
스크린샷 2022-02-05 오후 8 57 53
스크린샷 2022-02-05 오후 8 57 58
스크린샷 2022-02-05 오후 8 58 02
스크린샷 2022-02-05 오후 8 57 40

I took all the parts where the error appears, is this what you want?

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Feb 5, 2022
@sozohoy
Copy link
Author

sozohoy commented Feb 9, 2022

@pavel-ship-it
I'm sorry but is the above answer what ur looking for?

@pavel-ship-it
Copy link
Contributor

@sozohoy

  1. You are using 2 methods to import RealmSwift framework in the project - Swift Package Manager and Cocoa Pods. Please choose one.
  2. You can find a stack trace in the Xcode's Console. It's usually in the part of Xcode and it may look similar to that (see 'stacktrace & log')

@sozohoy
Copy link
Author

sozohoy commented Feb 9, 2022

@pavel-ship-it Here it is!

  • thread 1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff8)
    frame 0: 0x000000010259f3b8 uswTimeTable1.0`infoCourseData.addBtnClicked(sender=, self=0x0000000000000000) at infoCourseData.swift:0

    frame 1: 0x000000010259fa58 uswTimeTable1.0`@objc infoCourseData.addBtnClicked(_:) at :0

    frame 2: 0x000000011ddb09bc UIKitCore`-[UIApplication sendAction:to:from:forEvent:] + 96

    frame 3: 0x000000011d6b2064 UIKitCore`-[UIControl sendAction:to:forEvent:] + 124

    frame 4: 0x000000011d6b2448 UIKitCore`-[UIControl _sendActionsForEvents:withEvent:] + 348

    frame 5: 0x000000011d6ae8bc UIKitCore`-[UIButton _sendActionsForEvents:withEvent:] + 156

    frame 6: 0x000000011d6b0d80 UIKitCore`-[UIControl touchesEnded:withEvent:] + 532

    frame 7: 0x000000011ddeee80 UIKitCore`-[UIWindow _sendTouchesForEvent:] + 1104

    frame 8: 0x000000011ddf0978 UIKitCore`-[UIWindow sendEvent:] + 4352

    frame 9: 0x000000011ddc8be0 UIKitCore`-[UIApplication sendEvent:] + 788

    frame 10: 0x000000011de55078 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 7576

    frame 11: 0x000000011de5710c UIKitCore`__processEventQueue + 6792

    frame 12: 0x000000011de4f0ec UIKitCore`__eventFetcherSourceCallback + 184

    frame 13: 0x000000010afa92c0 CoreFoundation`CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 24

    frame 14: 0x000000010afa91c8 CoreFoundation`__CFRunLoopDoSource0 + 200

    frame 15: 0x000000010afa8554 CoreFoundation`__CFRunLoopDoSources0 + 256

    frame 16: 0x000000010afa2b9c CoreFoundation`__CFRunLoopRun + 744

    frame 17: 0x000000010afa23a8 CoreFoundation`CFRunLoopRunSpecific + 572

    frame 18: 0x0000000110a375ec GraphicsServices`GSEventRunModal + 160

    frame 19: 0x000000011ddaa7ac UIKitCore`-[UIApplication _run] + 992

    frame 20: 0x000000011ddaf2e8 UIKitCore`UIApplicationMain + 112

    frame 21: 0x0000000108953f9c libswiftUIKit.dylib`UIKit.UIApplicationMain(Swift.Int32, Swift.Optional<Swift.UnsafeMutablePointer<Swift.UnsafeMutablePointer<Swift.Int8>>>, Swift.Optional<Swift.String>, Swift.Optional<Swift.String>) -> Swift.Int32 + 100

    frame 22: 0x000000010259b7c8 uswTimeTable1.0`static UIApplicationDelegate.main() at :0

    frame 23: 0x000000010259b750 uswTimeTable1.

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Feb 9, 2022
@leemaguire
Copy link
Contributor

Hi @sozohoy, I took a look at your code for your app. Just a few pointers:

  • Use %@ instead of %s for arguments in NSPredicates. %s is only for c strings. So .filter("timetableName == %s", userData) becomes .filter("timetableName == %@", userData)
  • If you are using string interpolation for creating an NSPredicate string be sure to wrap the argument in quotes '. e.g
let searchText = "foo"
let testDB = realm.objects(testCourseData.self).filter("someStringProperty == '\(searchText)'")

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Feb 21, 2022
@sipersso
Copy link

sipersso commented Apr 1, 2022

I am also seeing something similar. The app crashes when running count on a filtered list. Only happens for some users here, not all.

@dianaafanador3
Copy link
Contributor

@sipersso can you attach the stack traces from this crashes?

@dianaafanador3
Copy link
Contributor

@sozohoy did you find the above response helpful?, are you still getting this error, or you were able to solve this?

@sipersso
Copy link

sipersso commented May 3, 2022

@dianaafanador3 in my case, I could trace it back to a corrupted realm file caused by bugs in the 10.22.0 realm version. It is the same issue as this one #7703

@dianaafanador3
Copy link
Contributor

Closing this as there is no response from the owner of this issue. @sozohoy if you still having issues, reopen this issue again.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
O-Community Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

6 participants