Change value after period of time in Firebase - swift

I have a lobby in which I want the users to be in sync. So when a user turns off his internet while the app is running, he should be removed. I know Firebase does not support server side coding, so the coding needs to be client side. The answers from How to delete firebase data after "n" days and Delete firebase data older than 2 hours do not answer this question since they expect that the user is online and they have an internet connection. So my question is if is possible to delete users when they got no internet? I thought maybe it is an idea to let the users update a value every 5 seconds, and when that update is not done, the other users in that lobby remove the player. This way is not good, since every player needs to retrieve and upload alot of data every 5 seconds. What is the best way to solve this?
Edit: to make it short, lets say each user has an image. The image should be green when the user is connected, and grey when disconnected.
Edit 2: after thinking it over, it is really hard to accurate present the connected users on a client-side server. That is why, if nobody has a different solution, I should add another server which can execute server-side codes. Because of the larges amount of servers, I would like to know which server I should use. The server should run a simple function which only checks if the users are connected or disconnected and can communicate with Firebase. If I am correct it should look like this:
But the server also needs to communicate with the users directly. I have absoluty no idea where to start.

If I'm not completely wrong, you should be able to use onDisconnect.
From the Firebase, documentation:
How onDisconnect:Works:
When an onDisconnect() operation is established, it lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the client if it is invalid. The server then monitors the connection. If at any point it times out, or is actively closed by the client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event.
In app in production I'm using onDisconnectRemoveValue, and when I close the app, the user removes himself from the lobby. Not sure how it works when you turn the device in airplane mode, but from the documentation it seems there should be no problem.
One thing: when you test it better do it on real device, the simulator have issues with turning it off and on, at least the on I have installed.
Edit: So i checked the onDisconnect when you put the device on airplane mode and it works! The question is, that it removes the user in about a 1:30 min, approximately, so if you read the documentation or ask the support, you may be (and only may be) able to find a way to set the time you want.

Related

How can I run server-side realtime database queries

I am using Firebase's Realtime Database.
I am able to use my old device and permanently keep it on for checking if the host has left a party to therefore notify the users. However, this is inconvenient and there must be a simple server side solution.
I know how to code it (using .observe etc.) but I don't know where to run the code. The code will be on a loop to check if a host has left every 10 seconds (this is because the host may run out of battery so the database is not notified). Can I simply run it in functions somehow? Or using hosting?
The server code will send a request to the host, and if there is no response, the party has therefore been closed so it will tell the users.
Any help or pointers in the right direction are greatly appreciated.
If you have any questions, please ask!
It's not related to the iOS. Put your initial code into the viewDidLoad or init methods (depends on how do you write the code) and forget about it. Those methods are called once per an instance. For now Firebase works fine on your usecase. At least I don't have any wierd updates on the observe method. also you can specify what do you want to observe exactly in the Firebase (something like the new or last 15)
The solution to this was that even if the user left the app, they would still be in the party. I used user defaults so it can remember if the user was in a party so it can return them.
I also used Realtime Database triggers which can remove all information about a user with one action in the app (so all data gets removed, and not left behind, which would create a waste of unusable database memory).

About server hosting, and how servers are set up

I have been making a game with javascript for a while now, and I am doing the websocketing with node.js and socket.io.
So my question is: Once I get a server, how would setting it up work? Would I send the people who own the server my files, and then would they take care of setting it up? Would I be able to access the computer they loaned me and set things up from there? I'm confused about the whole process... first time doing anything like this.

Can I to know if ipad was rebooted?

I am developing an application that must work online and also offline. This application should sync informations with our server. For this, we need that the device utilize the server clock.
I found a lot of information, and I get the following idea:
When the user logins online I will force him to get the server clock time. In this moment he obrigatory must have internet connection, so it is ok.
When I get the clock server time, I get the systemUptime information that says the interval that the device is turned on, and I store it. I can get systemUptime like this:
[NSProcessInfo processInfo].systemUptime
When the user to create a new local file, I will know the current interval based on systemUptime function, so I know the current time, and I don't depend the iOS system clock.
The problem is: Everytime that device is rebooted, or turned off, the systemUptime is reseted. Until here OK, I can solve it forcing the user to login again, and getting the server clock time again. My problem is to know when the device was rebooted. Can you help me? Thank you guys!
My advice would be to not refer to the device time at all. Get the the files from the server and have the server also answer the server time that the files are retrieved. You can store this in the file, in the file name, or separately on the device.
At some point in the future ask the server if there's a file newer than the server time you recorded earlier.
In this sense, the time isn't really a time at all, it's a version number, and you could make that explicit with the server too, using just an integer from the server that indicates progressing sequence of versions.
If it's important that your app be strong in this way, your only choice is to remain independent of device time. Otherwise, there are too many ways it can break (including small time errors due to latency on the time check, or device factory resets or malicious actions by a user). It's better to remain independent of device time if you can.
When you get the systemUptime the first time, subtract it from the current (iOS) date and time. That's the time the system was last brought up.
Then recalculate this value whenever necessary. If the "time the system was last brought up" has changed, then you know it's time to log in again.
However, as suggested by another commenter, I suspect there's a better way to tackle this from scratch.

Strategies on synching data and caching data between iphone and server

Say I have a TODO list iphone app, that can be edited/viewed from both a web application and the iphone application.
When on the iphone, when a user views all his todo lists, or sub-items, I would think that each time the user views a particular list it shouldn't be hitting the web applications API every-time, but rather cache locally the values and only hit the web when things change.
What strategies are there for this type of scenerio?
I agree with you in your dirty-otherwise-do-not-contact-the-server point. And I think this point is pretty straightforward and easy to implement.
However, be careful in this scenario: it gets dirty but at the same time, the device cannot reach the internet. In this scenario, I suggest you check the internet accessibility on a frequent basis (even when your app is in the background), and try to reach your server and update whenever possible.
This is a tricky problem. I'm currently working on an app that needs to perform a similar synchronization, and I haven't decided how I want to handle it yet.
You're right in that you don't want to be hitting the web repeatedly. It would slow the app down considerably. Keeping a local cache is the way to go.
One drawback is that the user could change/add an item on the web and you wouldn't see it on the phone. You'd need to have a refresh button (like in the Mail application, for example) to allow the user to get the changes.
Then you have an issue with conflict resolution. Say the same item is edited on both the phone and on the web. How does the user pick which one to keep, or do they get duplicated?
I think the best way to do this is to replicated your server's schema in CoreData. Then load a given element from the local DB, and in the background go out and check that element for updates if the device has an internet connection. You're hitting the db each time, but the user is not slowed down by the process.
You should not query the internet everytime you view the list.
But when you make updates to it, or edit it, you should update the server as well. That will make your life a whole lot simpler. That way when the user updates an item that he deleted in the web server, the server will just throw that request out...

get data from online once and then viewable offline

Okay, I want to have an app that takes phone numbers from an online database and displays them in a table view. When the user is not online, I want them to still be able to see the numbers they already got from the database in the table view. If the user manages to go back online, the database updates the view. My question is, is this possible to do and if so, what's the best way to approach it? (bit of a newbie, please help me out)
There are many ways to do what you are asking, depending on the complexity of what you are after.
Could I suggest the following steps (I'm not sure which ones you can do, and which ones you are having trouble with).
Connect to the server and retrieve the list of phone numbers
If the database has a web server front end this might be as simple as sending a get request to the server (see NSURLConnection) and parsing the result. Otherwise you will need to know/tell us what type database you are using.
Store the phone numbers on the device
Use SQLite to store the numbers on the device (See iPhone SQLite Resources)
Check for internet connectivity
Periodically check for internet connectivity, and if a specific time has elapsed since you last polled the server, retry. (See Checking iPhone internet connectivity)
Although you’re probably looking for a native app solution, you can also do this with a web app.
http://diveintohtml5.ep.io/offline.html
I am a new developer iPhone developer, "learning" to be precise. I came across the useful NSUserDefaults (a dictionary in which you can store/restore state even after your application relaunches). Problem with this dictionary will be memory in your case. NSUserDefaults is sort of global to all applications and yours may spoil the show for other innocent applications (like Weather :D ).
To work around this, you can have your application declare a property list file where you store a few numbers (best practise would be the most recent but you can use any selector of choice). Look for an appropriate time in your run loop to store these numbers into your property file and load them when the application starts.