Continue uploading NSData even connection lost/user clicked on home NSUrlConnection - iphone

I need to upload via HTTP Post NSData which is UIImage or NSUrl of a video file via the iPhone app which I develop for ios6
I need to support the following cases:
The user clicked on home - to continue uploading in background
The connection is lost - to continue uploading when there is a new internet connection even if the user left the app by clicking on the home button)
The user quit the app (clicked on x) and it was in the middle of uploading, next time that he would open the app, it will continue
In case 2 and 3:
It will continue from the same location that it was in the NSData (not start from the beginning of the file)
It will send in the new call a variable index={index} (where index is the amount of times that it continues starting from 0
I know how to use NSURLConnection as an async connection. But how to support the above 3 cases?

For this to work you need 2 things:
A record, stored persistently, of how much data has been uploaded to the server.
A server which accepts the Content-Range header and can handle storing partial uploads and completing them later. (or a server with an API to handle the same function).
If you have both of these things then you can perform the task using NSURLConnection and its delegate methods, NSUserDefaults (or similar) to store the progress information and subdataWithRange: (or perhaps NSFileHandle) to get only the data which needs to be uploaded.
Look at using connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: to get updates of what data has been sent. Store that number (if not all of the data has been sent) into user defaults. Once all the data has been sent, remove the number from user defaults.
Use subdataWithRange: by creating a range from the stored number to the (total length of data - the number).

Related

SwiftUI & Firestore

I am wanting to use Firestore to retrieve user info and other data linked to that user once they have logged in via firebase auth. On the home page of the app I use .onAppear{ pulluserData() }. I understand that Firestore functions are asynchronous so how can I wait for this data to be pulled before displaying it to the user on the home screen?
Here is my function to check the database:
func checkDatabase() async {
//Function that will check the database. Will be good to add a listener eventually
if self.pullUserData{
await dbm.readUser(userID: "VSWAq7QCw3dbGYwMdtClbbANGVe2")
}
}
and the actual database function:
func readUser(userID: String)async{
//Function that will be used to read user info from the database
let userRef = database.collection("users")
do{
let doc = try await userRef.document(userID).getDocument().data()
print("The doc is: ")
print(doc as Any)
}
catch {
}
}
There are a number of ways to do this but the two most common ways are to (1) use a launch screen to indicate a loading state that disappears to a view identical to the launch screen (to continue the appearance of loading) that is only removed when the database returns (i.e. Twitter); (2) load the user right into the app and allow them to move freely while either indicating that data is loading or displaying cached data.
Remember that Firestore maintains a local cache on the device which means that data will be available immediately when the app launches. This data may be out of sync with the server but it will update as soon as the app establishes a connection with Firestore, which is usually instant. What I would recommend is launching the user right into the app without the use of a loading screen and relying on the cached data to get the UI up as fast as possible.
And if we're only talking about user-specific data (data that is specific to the user that the user has full control over) then that data will only change when the user changes it, which would have been the last time they used the app, which means that the locally-cached data on their device (assuming they use only one device) will always reflect the state of the server (in theory, anyway). And if it doesn't then it doesn't; the fresh data will update instantly anyway.
You may then wonder what happens if the user launches the app without connection. In that case, the cached data is displayed and the user is almost none the wiser. And because Firestore is offline capable, the user can freely edit their data and it will write to the server when connection eventually establishes.

GWT panel fields data refresh delay on slow internet

I am using GWT 2.5.1.
In my GWT web app I have a ComplexPanel object which contains a set of fields(widgets). There are a suggested field (on panel) which gives me the opportunity to find object and info about it. Fields (10-15 of them) contain info about that object.
The problem is when user (client side) has a slow internet connection, fields on form are updated with a delay. And if in moment of delay user click 'Save' button (AsyncCallback), old data (which is not updated) posted to the server.
How it works:
1. Server receive callback from the form and start to processing the data.
2. Server refresh all the fields with new data and end the works.
3. Javascript updating the data on form using about 10 requests.
But: internet is slow and one part of data is refreshed and other no.
4. User click SAVE and mixed data goes to the server.
I need to know (from server side) when all the field are refreshed on client side and server can proceed with a next post request.
Thanks in any advice.
Perform a validation check on click of the save button. There are several things you can do as per your requirement. I have listed one way to validate it.
Set a flag before making the async call as false. For instance, isLoaded = false
Once you have all the fields just update it to true, i.e. isLoaded = true
On save button handler check for isLoaded flag. If false, prompt a message, else save.
Update:
You can count the number of response received. You know that you will get 10 response. So for every response received increment a counter. Activate save only if you have all 10 response recieved.
For a clean way to do this, use gwt-async-future.

iOS: How to save data over the complete lifecycle?

I have an iOS app with a login view.
I need to have the login-data the whole time until the user ends the app or click "logout".
How and where to store it?
Are something equel like "member variables" possible in iOS / objective C?
If you're going to be storing username/password data, it's probably best to store it in the keychain. If you're only looking to keep track of whether a user is logged in, then consider storing a flag of some kind in NSUserDefaults. You can clear data when the application exits through the applicationWillTerminate: method of your application delegate.
You could use NSUSerDefaults, but everything you save in NSUserDefaults stays there for ever unless you delete it. Id I understand i right, you want that the password ans login gets saved only when the app is opened, and when the app is being closed, that data gets deleted? With nsuserdefaults you will have to set #"" for the password and login in applicationwillterminate, to delete the data. Or you declare two NSStrings in you header file, and in the .m you do passwdstring = passwd.text
loginstring = login.text
what this does is it saves the data to two nsstrings, that data is available the whole time while the app is opened, when the user closes the app, and re opens it, the strings are nil again and when he logs in, the strings will have the loging info again.

How to get progress of sending image via bluetooth (Gamekit)?

I implement an application on iPhone which sharing photo via bluetooth(Gamekit).
I want to khow how to get a percentage of photo sending.
Please tell me.
Thanks
To keep under the maximum transferred data size you have to support breaking up your data.
Break up your data in, say, 10 fragments. Send each fragment in turn. Add up the percentages as each fragment is sent and recieved.
Alternatively have the recipient post back unreliably how much data has been received.

How to handle file uploads to a dedicated image server?

I got a webserver with a running application. There's a webpage with a form: some text data and a file upload field. Now, what I would like to have is it working like this:
The file is sent to the dedicated server, diffrent then the one application is running on. The server should return some kind of path (or anything that identifies the uploaded and saved file and allows to create an URL). Then, both this path and user-filled data should be submitted to the webserver with application, for any kind of database storage.
Problem is, there are 2 diffrent servers, so I can't upload the file with javascript, can I? Another way would be just to use iframe and put the upload form in there - but then I think I can't access the result of the upload (still inside the iframe) with javascript to pass the file path to my main server.
I could also just upload the file to same server my application is running on and then just rsync it to the other one - but I'd like to avoid it if I can, trying to minimalize the traffic actually :)
How do you handle such thing in your applications?
If you used an iframe, you could submit the upload form to the dedicated image server, and in the case of a successful result, have it in turn load a page from the original server with the info (eg. image path) "passed along" as a GET parameter.
POST to dedicated server, server stores image and calls back to web server through a web service or other to give it any info required.