RestKit - use identificationAttributes that are not part of the response - ios

In RestKit is it possible to use identificationAttributes that are actually not part of the JSON response?
My case is the following - I have a service that lists all articles for the currently logged-in user like http://example.com/json/articles.json
My problem is the following - since the application allows multiple users to login, I keep the articles in the database together with the userId for each article. If I set the articleMapping.identificationattributes = #["articleId"], then I have a problem if two users using the device have the same article - it will be overwritten regardless of the userId, because it is not part of the response.
To sum up the facts:
For the JSON request I do not send the userId, it is part of the
server session only, so I think that I cannot use RKRoute
I do the mapping of the article with the user manually after RestKit mapping.
I do not have the userId property as part of the JSON response, it exists only inside the ArticleManagedObject.
Is there a way to inform RestKit that during the mapping, it should check the articleId+userId combination as an identificator? I tried using identificationPredicate with no success.
EDIT:
An example response from the server, when UserA is logged in:
{
"data":{
"articles":[
{
"articleId":1,
"title":"Objective C Basics"
},
{
"articleId":2,
"title":"Xcode Basics"
}
]
}
}
and here is the response when UserB is logged in:
{
"data":{
"articles":[
{
"articleId":1,
"title":"Objective C Basics"
},
{
"articleId":3,
"title":"Java Basics"
}
]
}
}
If UserA logs in, everything is fine. But if UserB logs in from the same device, then article 1 is mapped to UserB, and from now on, the connection between UserA and article 1 is lost.
As I understand from your suggestion, the only solution is to return also the user id from the service, set RKUnionAssignmentPolicy and let RestKit take care of the mapping (currently I am manually making the mapping between articles and users after RestKit).
Another question that I have - is it possible to set the identificationAttributes or identificationPredicate so that it makes a separation between object article 1 for UserA and object article 1 for UserB.

You currently do the user to article mapping outside RestKit, this is fine, but you will need to modify this process a little.
To begin with, I'm assuming here that the article response is the full set of articles for the user. If not then things get more tricky and you'll need to modify the below to account:
Start by getting all of the existing articles for a user. With this we're going to look at what needs to be removed and what needs to be added.
As we iterate through the articles we have received we can check the existing articles for a match, if we find one we have no work to do. If we don't find a match we need to add the relationship to the existing set, which will be a union with any relationship to any other user.
Next we want to remove the list of new articles from the list of the existing articles to get the list of deletions, for these we just need to break the link, again leaving other users unchanged.

Related

Realm Swift: Question about Query-based public database

I’ve seen all around the documentation that Query-based sync is deprecated, so I’m wondering how should I got about my situation:
In my app (using Realm Cloud), I have a list of User objects with some information about each user, like their username. Upon user login (using Firebase), I need to check the whole User database to see if their username is unique. If I make this common realm using Full Sync, then all the users would synchronize and cache the whole database for each change right? How can I prevent that, if I only want the users to get a list of other users’ information at a certain point, without caching or re-synchronizing anything?
I know it's a possible duplicate of this question, but things have probably changed in four years.
The new MongoDB Realm gives you access to server level functions. This feature would allow you to query the list of existing users (for example) for a specific user name and return true if found or false if not (there are other options as well).
Check out the Functions documentation and there are some examples of how to call it from macOS/iOS in the Call a function section
I don't know the use case or what your objects look like but an example function to calculate a sum would like something like this. This sums the first two elements in the array and returns their result;
your_realm_app.functions.sum([1, 2]) { sum, error in
if let err = error {
print(err.localizedDescription)
return
}
if case let .double(x) = result {
print(x)
}
}

Get firebase message from sender and receiver

I am using firebase for simple chat application. I am looking to fetch messages of user 1 and user 2. My database structure look like this.
I am using queryOrdered to fetch messages but it will filter with either sender or receiver.
Database.database().reference().child("Chats").queryOrdered(byChild: "receiver")
.queryEqual(toValue: "2WCS7T8dzzNOdhEtsa8jnlbhrl12")
.observe(.value, with: { snapshot in
})
But using this code I got messages of receiver only. How can I receive messages of sender as well. Can I use AND condition to query sender child?
There is no way in your current data structure to with a single query get both the messages that a specific user sent and received.
Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property.
For example, you could combine the sender and receiver UID into a single properties "sender_receiver": "2WCS7T8dzzNOdhEtsa8jnlbhrl12_Sl91z...." and then query for that combined value.
For a longer example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase
For most chat applications however I recommend creating a database structure that models chat "rooms". So instead of having one long list of all messages, store the messages in rooms/threads, which is also what you show in the UI of your app.
"-RoomId1": {
"msg1": { ... },
"msg2": { ... }
}, "-RoomId2": {
"msg3": { ... },
"msg4": { ... }
}
That way you have the most direct mapping from the database, to what you show on the screen.
For a good way to name the "rooms", see Best way to manage Chat channels in Firebase
You'd then still store a mapping for each user of what rooms they're a part of, which might also be useful in securing access.

How to realize users feedback with FireBase?

I have an application where user can like photos, comment etc. Functionality like Instagram has.
I want to realize users !feedback!, where user can see information, who liked his photos, who started to follow and etc. I don't know actually how should I organize structure of my database in this situation.
My user node snapshot:
My posts node snapshot:
As I can see, I have next option - I should save all actions, which are linked to user, to his node in internal node Feedback. But how can I keep sync this? For example, someone can follow my user, I will add it to this node, user will unfollow, but the record still remains. I think, that it is wrong way.
I have no other idea actually and I can't find anything about that.
Any suggestions and solutions are much appreciated.
EDIT: I need to understand, how to realize this tab of instagram-like apps:
How to retrieve data for it from nodes?
UPD: DB Architecture in my examples is bad (old question). Be carefull (10.11.2017).
First, let's think about how we need to structure our database for this:
There are two very important principles to follow when structuring data for Firebase:
You should save your data the way you want to retrieve it.
You should keep your data structure as flat as possible - avoid nesting.
Point 1 is because Firebase is not a relational database. This means that we need to keep queries simple in order to achieve performance. Making complex queries might require many requests to Firebase.
Point 2 is because of the way Firebase's query model works: If you observe a node, you also get all the children of that node. This means that, if your data is deeply nested, you might get a lot of data you don't need.
So, having those principles in mind, let's take a look at your case. We have users, who have photos. These are the two primary entities of your database.
I can see that, currently, you are keeping your photos as properties of the users. If you want to be able to query photos by user quickly (remember Point 1), this is a good way to do it. However, if we want users to be able to "favorite" photos, a photo should be more than just a link to its Firebase Storage location: It should also hold other properties, such as which users have favorited it. This property should be an array of user IDs. In addition, for each user, you'll want to store which photos are that user's favorites. This might seem like data duplication, but when using Firebase, it's OK to duplicate some data if it'll lead to simpler queries.
So, using a data index such as in the example above, each of your Photos should look like this:
{
id: /* some ID */,
location: /* Firebase Storage URL */,
favorited_by: {
/* some user ID */: true /* this value doesn't matter */,
/* another user ID */: true,
},
/* other properties... */
}
And your user should have a favorites property listing photo IDs. Now, since every photo has a user that "owns" it, we don't need to have a unique ID for every photo, we just need to ensure that no user has two photos with the same ID. This way, we can refer to the photo by a combination of its user ID and its photo ID.
Of course, remember Point 1: If you want to be able to get user info without getting a user's photos, you should have a different property on your root object for photos instead of associating photos with users. However, for this answer, I'll try to stick to your current model.
Based on what I said above, the favorites property of a user would hold an array of values of the format 'userId/photoId'. So, for instance, if a user favorites the photo with ID "3A" of the user with ID "CN7v0A2", their favorites array would hold the value 'CN7v0A2/3A'. This concludes our structure for favorites.
Now, let's look at what some operations you have mentioned would look like under this structure:
User favorites a photo:
We get the user ID of the photo's owner
We get the user ID of the user who is favoriting the photo
We get the ID of the photo
We add the user who is favoriting's ID to the photo's favorited_by array
We add photoOwnerID + "/" photoID to the favoriting user's favorites array
If the user unfavorites the photo later, we just do the opposite: We remove photoOwnerID + "/" + photoID from the user's favorites and we remove the favoriting user's ID from the photo's favorited_by property.
This kind of logic is sufficient to implement likes, favorites, and follows. Both the follower/liker/favoriter and the followee/likee/favoritee should hold references to the other party's ID, and you should encapsulate the "like/favorite/follow" and "unlike/favorite/unfollow" operations so that they keep that database state consistent every time (this way, you won't run into any issues such as the case you mentioned, where a user unfollows an user but the database still holds the "following" record).
Finally, here's some code of how you could do the "Favorite" and "Unfavorite" operations, assuming you have a User model class:
extension User {
func follow(_ otherUser: User) {
let ref = FIRDatabase.database().reference()
ref.child("users/\(otherUser.userId)/followers/")
.child(self.userId).setValue(true)
ref.child("user/\(self.userId)/following/")
.child(otherUser.userId).setValue(true)
}
func unfollow(_ otherUser: User) {
let ref = FIRDatabase.database().reference()
ref.child("users/\(otherUser.userId)/followers/")
.child(self.userId).remove()
ref.child("user/\(self.userId)/following/")
.child(otherUser.userId).remove()
}
}
Using this model, you can get all the follower user IDs for a user querying that user's followers property and using the .keys() method on the resulting snapshot, and conversely for users a given user follows.
Added content: We can build further on this structure in order to add simple logging of actions, which seems to be what you want to have available to the user in the "Feedback" tab. Let's assume we have a set of actions, such as liking, favoriting and following, which we want to show feedback for.
We'll follow point 1 once again: In order to structure feedback data, it is best to store this data in the same way we want to retrieve it. In this case, we will be most often showing a user their own feedback data. This means we should probably store feedback data by user ID. Additionally, following point 2, we should store feedback data as its own table, instead of adding it to the user records. So we should make a new table on our root object, where for each user ID, we store a list of feedback entries.
It should look something like this:
{
feedback: {
userId1: /* this would be an actual user ID */ {
autoId1: /* generated using Firebase childByAutoId */ {
type: 'follow',
from: /* follower ID */,
timestamp: /* Unix time */,
},
autoId2: {
type: 'favorite',
from: /* ID of the user who favorited the photo */
on: /* photo ID */
timestamp: /* Unix time */
},
/* ...other feedback items */
},
userId2: { /* ...feedback items for other user */ },
/* ...other user's entries */
},
/* other top-level tables */
}
In addition, we will need to change the favorites/likes/follows tables. Before, we were just storing true in order to signal that someone liked or favorited a photo or followed a user. But since the value we use is irrelevant, as we only check keys to find what the user has favorited or liked and who they have followed, we can start using the ID of the entry for the like/favorite/follow. So we would change our "follow" logic to this:
extension User {
func makeFollowFeedbackEntry() -> [String: Any] {
return [
"type": "follow",
"from": self.userId,
"timestamp": UInt64(Date().timeIntervalSince1970)
]
}
func follow(_ otherUser: User) {
let otherId = otherUser.userId
let ref = FIRDatabase.database().reference()
let feedbackRef = ref.child("feedback/\(otherId)").childByAutoId()
let feedbackEntry = makeFollowFeedbackEntry(for: otherId)
feedbackRef.setValue(feedbackEntry)
feedbackRef.setPriority(UInt64.max - feedbackEntry["timestamp"])
let feedbackKey = feedbackRef.key
ref.child("users/\(otherUser.userId)/followers/")
.child(self.userId).setValue(feedbackKey)
ref.child("user/\(self.userId)/following/")
.child(otherUser.userId).setValue(feedbackKey)
}
func unfollow(_ otherUser: User, completionHandler: () -> ()) {
let ref = FIRDatabase.database().reference()
let followerRef = ref.child("users/\(otherUser.userId)/followers/")
.child(self.userId)
let followingRef = ref.child("user/\(self.userId)/following/")
.child(otherUser.userId)
followerRef.observeSingleEvent(of: .value, with: { snapshot in
if let followFeedbackKey = snapshot.value! as? String {
// we have an associated follow entry, delete it
ref.child("feedback").child(otherUser.userId + "/" + followFeedbackKey).remove()
} // if the key wasn't a string, there is no follow entry
followerRef.remove()
followingRef.remove()
completionHandler()
})
}
}
This way, we can get a user's "feedback" just by reading the "feedback" table entry with that user's ID, and since we used setPriority, it will be sorted by the most recent entries first, meaning we can use Firebase's queryLimited(toFirst:) to get only the most recent feedback. When a user unfollows, we can easily delete the feedback entry which informed the user that they had been followed. You can also easily add extra fields to store whether the feedback entry has been read, etc.
And even if you were using the other model before (setting "followerId" to true), you can still use feedback entries for new entries, just check if the value as "followerId" is a string as I have done above :)
You can use this same logic, just with different fields in the entry, to handle favorites and likes. When you handle it in order to show data to the user, just check the string in the "type" field to know what kind of feedback to show. And finally, it should be easy to add extra fields to each feedback entry in order to store, for instance, whether the user has seen the feedback already or not.
You can sort of implement what you want by using Firebase Functions. Here's roughly how I would go about implementing it:
All a user's feedback will be stored in /Feedback/userID/, located at the root.
Within this node, have a subnode called eventStream.
Whenever an action occurs, this can be directly added to the user's eventStream, ordered by time.
This action could be of the form: pushID: { actionType:"liked", post:"somePostID", byUser:"someUserId" }
Also include an anti-action subnode (under /Feedback/userID/). Whenever one of these 'anti-action' events occurs (for example: unlike, unfollow etc.), store this under the anti-action node for the corresponding user. This node will essentially act as a buffer for our function to read from.
This anti-action could be of an almost identical form: pushID: { actionType:"unliked", post:"somePostID", byUser:"someUserId" }
Now for the function.
Whenever an anti-action is added to the anti-action node, a function removes this from the anti-action node, finds the corresponding action in the eventStream, and removes this. This can be achieved easily by first querying by "actionType" then "someUserId" and then by "somePostID".
This will ensure that the user's eventStream will always be up to date with the latest events.
Hope this helps! :)

Firebase rules to access specific child's value

The childByAutoId would be useful if you want to save in a node multiple children of the same type, that way each child will have its own unique identifier.
List:{
KJHBJJHB:{
name:List-1,
owner:John Doe,
user_id:<Fire base generated User_id>
},
KhBHJBJjJ:{
name:List-2,
owner:Jane Lannister,
user_id:<Fire base generated User_id>
},
KhBHJZJjZ:{
name:List-3,
owner:John Doe,
user_id:<Fire base generated User_id>
}
}
I am trying to access the List with the help of the following code:
let ref = FIRDatabase.database().reference(withPath: "/List")
The current user logged into the app is John Doe. When the user accesses the list, I want all the List child whose owner is John Doe(i.e. List-1 & List-3) and ignore the other child values.
Do I have to do this in my application or can this be achieved via Firebase Security rules?
My current rule definition is:
"List":{
".read": "root.child('List/'+root.child('List').val()+'/user_id').val() === auth.uid" }
But this rule is not giving me any success. Any idea how to achieve the desired result?
You're trying to use security rules to filter the list. This is not possible and one of the common pitfalls for developers coming to Firebase from a SQL background. We commonly refer to it as "rules are not filters" and you can learn more about it in:
the Firebase documentation
this answer
our new video series Firebase for SQL developers
and many previous questions mentioning "rules are not filters"
The solution is almost always the same: keep a separate list of the keys of posts that each user has access to.
UserLists:{
JohnUid: {
KJHBJJHB: true,
KhBHJZJjZ: true
},
JaneUid: {
KhBHJBJjJ: true
}
}
This type of list is often referred to as an index, since it contains references to the actual post. You can also find more about this structure in the Firebase documentation on structuring data.

updating many users at once in parse

I'd like to update user's column which presents related posts that the user might like,
my code is like that:
let users = query.findObjects() as [PFUser]
for user in users{
let rel = user.relationForKey("posts")
rel.addObject(post, forKey: "relatedPosts")
rel.saveInBackground()
}
I really don't know why, but I tried to do that in many versions (not only by relation, also by arrays and other methods) and it always updates just one user (the one who logged in)..
how can I get this done?
You can't update user that is not currently authenticated in your app.
The way I see it you have 2 options:
1) You can set some Cloud Code to run using the master key, so it can modify users.
2) You can add a new custom column to your Parse User class that will link to another class DB that has the related posts for the user.
take a look here: Parse.com Relations Guide
I would choose #2.

Resources