I am Tim Corey. I teach coding online and my inbox is full of questions from current and future developers. I wish I could sit down with each of you and share the answers I know will help you go further, faster in the world of development. This podcast is the next best thing. On this podcast, I will answer the biggest questions people are asking! Send us to your suggestion for a future Dev Questions at https://suggestions.iamtimcorey.com/ To keep the podcast coming, like, subscribe, rate, and share it with your friends and colleagues. See why thousands of students have chosen to learn to think and code like a professional developer at www.DevForge.com.
Use Left/Right to seek, Home/End to jump to start or end. Hold shift to jump forward or backward.
0:00
|
11:51
How do I work with offline data? How do I store it? How do I sync my data when my application comes back online? How do I deal with applications that go offline periodically? These are the questions we will answer in today's episode of Dev Questions.
Welcome to the Dev Questions Podcast with Tim Corey. Join us each episode as we tackle the questions you are asking about a career in software development, understanding the industry, and new technology. If you're just starting out or you want to grow stronger as a developer, this is the place to get your questions answered. Now, here's your host, expert developer and online educator, Tim Corey.
SPEAKER_02
How do I work with offline data? Say my app goes offline online throughout the day or the week, and I need to make sure I have data even when I'm not connected. How do I do that? This is a question that came up on the suggestion site, and it's one I want to tackle in today's episode of DevQuestions. Now, if you have a question, go to suggestions.imtimcorey.com, ask your question there, and hopefully you'll see it answered in a future episode of DevQuesti. So, working with data offline in your application, how do you do it? Well, you're gonna love this answer. You probably expect it to come. It depends. So we can talk first about tools because that yeah, there are some tools to help you. For example, SQL Lite databases work locally. You can install those with your application. You can use PouchDB if you're using a web application because PouchDB has a synchronization setup. Or the Azure Mobile App SDK also has the ability to synchronize data for you. But those tools aren't really the important part about offline data because the tooling isn't the problem. So let's talk first about the types of offline data. Yes, there's more than one type. So the first type is the rarely changing data. Let's say you have a website that is also a progressive web app that you use offline. Well, maybe it's for a pizza place, and you have the menu and the hours of operation, and you make that site work offline. Really, all you're doing is making those pages work offline. And yeah, you have some data, but it's just hours of operation and menu items, and those don't change that often. No big deal. Okay. But then you have kind of level two is you want to have some offline data that you want to use more heavily. For example, um having a read-only copy of your data. Maybe you want to have lookup data so you can look up information and access information if you are trying to figure out if a person is allowed to be somewhere. You could have a scanner that scans their badge and then looks up in a read-only list all the attendees and say, yes, they're allowed to be here. That's kind of a read-only data setup. But then the kind of level three, the next level up of complexity, is if you want to create new data. So we're just talking about creating new data and having kind of the previous stuff too, the the uh occasional data as well as the read, the full read lookup. So now you want to create data. Think of a point of sale system. Maybe you have a um a pop-up shop somewhere, and you have your your ability to check out on this point of sale system, and you know, you don't always have connectivity. It's okay. You can scan the items into the into the app, and you can say, yes, go ahead and you know, we we accepted the money, and so we're good. Here's even the receipt, and we're good. We do have a new purchase, it's a brand new thing, it doesn't have to in any way interact with any other data. That's kind of level three. Level four gets a little more complex than that, where you have narrow updates. Um, let's, for example, um let's say you have a help desk where you have to physically go to different locations and do work. And you you may not always be connected. And so when you're doing a ticket, you might say, I'm gonna work on ticket one, two, three. And so you you check it out in your name, and then you go out in the field and you do work on it, but you're offline. Well, you have all that data, that all the read-only data, you have the ability to create new tickets, and you have the ability to update the ticket you're working on because no one else is gonna update that ticket. In theory, um, when I lived in Pennsylvania, this is what Comcast did when they would come out to work on my house. So they were the internet provider. When you're working on internet, you don't always have internet. And so they had an offline system, it was a progressive web application, where they would say, these are the tickets I'm gonna work on today. And when they got to a residence, they might not have cell coverage. And so they could still do the work, they still had all the information about my house and about my setup. They had the ticket, and they have the ability to close the ticket where they could take pictures of what they did, they can explain what they did, they can have all the information documented, they could even have my sign-off on it, and then they could complete the ticket, and then at the end of the day, when they got back into cell range or back to the office, whatever it was, it would synchronize that data. That's narrow updates. Now, this last level we're gonna talk about, level five, is complex update of data. So this is this is not just if you're going to have a help desk ticket where one person works on it. This is what if you have multiple different locations all working on the same data? Think of Dropbox. So I use Dropbox for some of my files. Now I use other things too, like OneDrive and all the rest. But with Dropbox, I can have Dropbox work offline. That's part of the features of it. So it synchronizes my files down to my laptop, my desktop, even my phone. Well, when it synchronizes, let's say, to my my desktop and my laptop. And then let's just say that the internet goes out of my house. Happens once in a while. Well, I can make a change on a on a file on my laptop. Let's say I have a Word document open and I add one new bullet point to my bullet point list. Well, then I go over and I save it. Then I go over to my desktop computer and I go, oh, there's something else in that file. I open that file up and I make a change to a paragraph at the bottom. Now the internet comes back online. What happens to that file? Well, I've got changes from my desktop, I've got changes from my laptop. You have to choose how do you do this? How do you handle the fact that there's changed files from two different locations? Now, there's a date and time on those changes. Is it as simple as take the last changes? Well, in this case, no, because I made a different change on my desktop than I did on my laptop. So if it took the latest changes, it would lose that bullet point that I wanted to keep. And if it took the first changes, it would keep the bullet point, but lose the change of the paragraph. And it can't just merge them because Dropbox doesn't understand how to look at a Word document and do line-by-line changes. It's not a text document. So what does it do? Well, that's where what's called a merge conflict. And this is the big deal about synchronizing data offline is you got to think through your process. What do you want to happen when you have a conflict? Because you can get you can have systems, you can get SQLite, you can get uh PouchDB, you can get these other tools that will synchronize your data for you, but you have to start making choices. You could have your own database type, you could use MySQL, whatever, but you have to figure out what's the process. So, first of all, what are you gonna permit? So when you design your business logic, you have to say, okay, we're gonna allow offline data, but what kind of data? Level one, level two, level five? Where's the where's the level for us of what we're going to allow? And then how do we handle conflicts? Because conflicts are going to be what syncs your application if you don't plan for them. And you can't just say, well, the software handles that. The software can't. You have to make a choice. You have to figure out ahead of time how do you handle it? In the case of a Word document, you can't just say, well, let the software handle it. There is no solution from the software perspective. What you have to do is tell the user, we have a conflict. Here's the two versions. What do I do? And allow the user to make a choice. And is that okay? What user do you tell? And is there going to be a problem with that? How do you show that? How do you handle changes in the meantime? And so many other questions you have to think through. Offline data is tricky. Getting it right is important. And it's something that doesn't just come from buying a certain software or implementing a certain software. You need to think through and create your business logic for how to do as well. So your choice will make this either simple or complex. If you choose, we're only going to have a read-only copy local, that's easy. You can do that, no problem. Because then all you do is when you come back online, look for new data and download it. Cool, no problem. If you want to make some simple changes like creating new records, that's usually okay. The only time you'll have a problem is if you have an incremental ID in a database and you create record five, and there's already record five in the database because somebody created it while you were offline. That's when you figure out what to do. But so that's a conflict, but it's not a big deal because you can figure out how do I handle this? It might be as simple as saying, we'll create the IDs once we get to the to the online database. So, or you use GUIDs, or there's a number of other things you can do. But what do you do when you have a conflict? That's yours to decide, and it's yours to work out, and it's yours to write the code for. There's not really a one-size-fits-all solution to handling this. Okay, so that's my answer to the question: what do you do about offline data in your application? Thanks for listening, and as always, I am Tim Corey.
SPEAKER_01
Thank you for joining us for this episode of Dev Questions. Tim is committed to making it easier for you to become a developer. If you would like to help make more content like this possible, please like, subscribe, rate, and share Dev Questions. You can also send your questions to questions at IamTimcorey.com. Until next time, remember, you are too smart and your time too valuable to waste it making all the mistakes Tim did. When you're ready to learn to think and code like a professional developer, head over to IamtimCorey.com and enroll in a course.