Why can't I get the index of a filtered realm List?
I figured out the solution. The filter syntax I used .filter({ $0.title == "Child" })
isn't the Realm filter, and converts the List to a LazyFilterCollection<List<Model>>
, which doesn't seem to be compatible with searching for the index of a realm object.
The fix was to use the format .filter("title == %@", "Child")
, which returns a realm Results object.
Filtering results from Realm
You can do this way:
Realm is not setup in my project so i am doing with struct.
Struct of Location:
struct Locations {
let latitude: Double
let longitude: Double
}
// Your Realm result Array
var theLocations = [Locations]()
// Adding Dummy Data into theLocations Array
theLocations.append(Locations(latitude: 37.33454847, longitude: -122.03611286))
theLocations.append(Locations(latitude: 37.33454218, longitude: -122.03638578))
// latitude and longitude to store values
var arrLat = [Double]()
var arrLong = [Double]()
// Looping through theLocations Array and Seperate latitude and longitude to append to array
theLocations.forEach{ location in
arrLat.append(location.latitude)
arrLong.append(location.longitude)
}
Hope this will help you.
Update the list when data is filtered from the realm database in android
Ok so finally found a solution to the about question after expermenting various things. All you need to do is call updateData() method of the RealmRecyclerViewAdapter which you will extend to your normal recyclerview.
Below code snippet provide the solution.
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm) {
userModels = realm.where(UserModel.class).greaterThanOrEqualTo("age", 20).findAll();
}
});
dataListAdapter.updateData(userModels);
The main point to keep in mind in above code is that you have to notify data outside the transaction block. If not done outside block, app will crash stating you cannot notify data when transaction is running.
Hope it helps someone somewhere.
Happy coding!!!
How to filter a realm subclass swift
There are actually multiple questions here. First we need to change the use of of the word subclass
to instance
. There are no subclasses in the question.
Note that Realm is conceptually an `Object' database so we're going to use the word 'object' to refer the objects stored in Realm (the 'instances' when they've been loaded in to memory)
I will rephrase each for clarity:
- How do I return all
SubscriptionClass
objects?
let results = realm.objects(SubscriptionClass.self)
- How do I return one specific
SubscriptionClass
object if I know the
primary key?
let oneObject = realm.object(ofType: SubscriptionClass.self, forPrimaryKey: theObjectsPrimaryKey)
- How do I return all of the
SubscriptionClass
objects that match a
specific criteria; where the question property equalsWhat is 5*5
let results = realm.objects(SubscriptionClass.self).filter("question == 'What is 5*5'")
and I will slide in #4: this queries for all questions as in #3 but only returns the very first one it finds. This can be dangerous because Realm objects are unordered so if there are ever more than one object, it could return a different first object each time it's used
if let oneObject = realm.objects(SubscriptionClass.self).filter("question == 'What is 5*5'").first {
//do something with the object
}
Subclass:
A subclass inherits the superclass's functionality and extends it to provide additional functionality. Here's a subclass that adds a difficulty property to a subclass of SubscriptionClass (the 'super class')
class SubscriptionWithDifficultySubClass: SubscriptionClass {
var difficulty = "" //Easy, Medium, Hard etc
}
and then a different subclass that provides a subject property:
class SubscriptionWithSubjectSubClass: SubcriptionClass {
var subject = "" //Math, Grammar, Science etc
}
Realm Swift - How to remove an item at a specific index position?
Results
are auto-updating and you cannot directly modify them. You need to update/add/delete objects in your Realm
to effect the state of your Results
instance.
So you can simply grab the element you need from your Results
instance, delete it from Realm
and it will be removed from the Results
as well.
Assuming the Results
instance shown in your question is stored in a variable called recipes
, you can do something like the following:
let recipeToDelete = recipes.filter("id == %@","04b8d81b9e614f1ebb6de41cb0e64432")
try! realm.write {
realm.delete(recipeToDelete)
}
Filter an object on a nested array in Realm Swift
If I understand correctly, I think a query like this will do what you're after:
func getFriendChatConversationModel(_ friendID: String) -> ChatConversationModel? {
let realm = try! Realm()
let chatConversationModels = realm.objects(ChatConversationModel.self).filter("typeIndex = %@", ChatTypes.ChatType.oneToOne.index)
return chatConversationModels.filter("ANY userAcitivities.userID == %@", friendID).first
}
How to filter object from Realm database in Swift
You are using the wrong operator in a realm, please check my answer below.
for item in realm.objects(data.self).filter("id == 2 AND user_id == 4") {
print(item)
}
Related Topics
Extending Dictionary with Key and Value Constraints
How to Read a File from The Filesystem in a Swift Command Line App
Macos Swift UI View Where There Is a Search Field in The Title Bar
Prevent Nscollectionview 'Lifting' an Item During Drag
How to Use The Spritekit Method Body(At: Cgpoint)
Animation Triggered Using a Button Stops a Repeatforever Animation Added Onappear
How to Check for Self with Contains() in Swift
Getting an Ip Address and Port Number from a Sockaddr_In Struct in Swift
Validate Unicode Code Point in Swift
How to Pass Data Backwards to a View Controller After Passing Forward
What's The How to Access a Swift Package Item from Objective-C
Core Image and Memory Leak, Swift 3.0
Populating Collection View from Array in Document Directory
Dynamic UIcollectionviewcell Width with Respect to Frame Width and Aspect Ratio
What Is The Intended Use of Optional Variable/Constant in Swift