Time for action – creating a data save stack

We're going to make a copy of a stack, but only if another copy of that stack doesn't exist. LiveCode has a nice if there is a… function, which was made for times like this!

First, we will create the stacks we'll need by following these steps:

  1. Start a new Mainstack with the name LaunchStack. Save it somewhere other than your computer's Documents folder.
  2. Start another new Mainstack with the name AppStack. Save it in the same folder as the first stack.
  3. Place some data on each stack's card, so that you can easily recognize when you're in that stack. For example, drag a button onto the card of the LaunchStack stack and name it in a way that makes it very easy to recognize. Do the same for the AppStack stack.
  4. Put the following openStack handler in the stack script of LaunchStack:
    on openStack
      set the defaultFolder to specialFolderPath("Documents")
      if there is not a file "AppStack.livecode" then
      put the filename of this stack into masterfile
        set the itemdelimiter to "/"
        put "AppStack.livecode" into the last item of masterfile
        --put specialFolderPath("engine") & "/AppStack.livecode" into masterfile
        put specialFolderPath("Documents") & "/AppStack.livecode" into appfile
        put URL ("binfile:" & masterfile) into URL ("binfile:" & appfile)
      end if
      go stack specialFolderPath("Documents") & "/AppStack.livecode"
    answer the filename of this stack
    end openStack
  5. Save both the stacks and quit LiveCode.

Before trying the stacks on a device or in the simulator, we'll try them as desktop stacks by following these steps:

  1. Look in your Documents folder; there should not be an AppStack.livecode file in this folder at the moment.
  2. Launch LiveCode by double-clicking on the LaunchStack.livecode file. If you find that LiveCode doesn't launch this way, make sure that you have the associated .livecode documents to be opened with LiveCode. If you are using more than one copy of LiveCode, say you're trying the Community version and the Commercial version, you can drag the stack file onto the copy of LiveCode that you're intending to use.
  3. Look in your Documents folder; there now should be an AppStack.livecode file with the time when the file was created that matches the current time.
  4. You should also see that the path to AppStack is indeed in your Documents folder.

Now, follow these steps to try our stacks on a mobile device or an iOS Simulator:

  1. Close the AppStack stack and uncomment the put specialFolderPath… line from the LaunchStack stack script that you entered in step 4.
  2. Go to Standalone Application Settings and choose the Copy Files section.
  3. Click on Add File… and locate and add the original AppStack.livecode stack (not the one that was created with the previous test).
  4. Choose either the Android or iOS section of the Standalone Application Settings and check the box to make the app available for that platform.
  5. From the Development menu, select your test target. That would be either one of the iOS simulators, if you choose iOS, or the connected Android device.
  6. Select Test from the Development menu. You should now be able to view your AppStack and an alert dialog box showing the path to the stack. The following screenshot shows the resulting dialog box in the iOS Simulator window and in an Android 4 tablet:
    Time for action – creating a data save stack

What just happened?

We set up our app to copy the main application stack in the documents area on the device, so that we'll be able to make changes and save those successfully. If you happen to test on iOS and Android, you will see quite different looking paths for the stack. LiveCode takes care of finding these special folders for us.

Pop quiz – other special places

Check whether you just happen to know this or use this question as an excuse to read the release notes and dictionary! Which of these is not a specialFolderPath type?

  1. Users
  2. Home
  3. Desktop
  4. 0x000e

Answer: 1

The specialFolderPath types Home and Desktop are not used by Android and Desktop is not used by iOS. 0x000e sounds suspicious, but is actually the specialFolderPath entry for My Videos under Unix! None of the systems have a Users entry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.250.114