iOS Question Save list with KeyValueStore

angel_

Well-Known Member
Licensed User
Longtime User
I am trying to save a list with KeyValueStore but I am getting the following error:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    
    Private kvs As KeyValueStore
    Private ListStoreData As List
    Private B4XComboBox1 As B4XComboBox
    Private B4XFloatTextField1 As B4XFloatTextField
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    
    kvs.Initialize(xui.DefaultFolder, "store")
    
    ListStoreData.Initialize
    ListStoreData = kvs.Get("ListaElementos")
    
    B4XComboBox1.SetItems(ListStoreData)
End Sub

Sub Button1_Click
    ListStoreData.Add(B4XFloatTextField1.Text)
    kvs.Put("ListaElementos", ListStoreData)
    B4XComboBox1.SetItems(ListStoreData)
    B4XFloatTextField1.Text = ""
    xui.MsgboxAsync("Saved", "B4X")
End Sub
B4X:
Copying updated assets files (9)
Application_Start
Error occurred on line: 69 (B4XComboBox)
Object was not initialized (NSArray)
Stack Trace: (
  CoreFoundation       <redacted> + 252
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       <redacted> + 0
  B4i Example          -[B4IObjectWrapper object] + 120
  B4i Example          -[B4IList AddAll:] + 100
  B4i Example          -[b4i_b4xcombobox _setitems::] + 588
  B4i Example          -[b4i_main _application_start:] + 1316
  CoreFoundation       <redacted> + 144
  CoreFoundation       <redacted> + 292
  B4i Example          +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1400
 B4i Example          -[B4IShell runMethod:] + 420
 B4i Example          -[B4IShell raiseEventImpl:method:args::] + 1968
 B4i Example          -[B4IShellBI raiseEvent:event:params:] + 1360
 B4i Example          __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
 libdispatch.dylib    <redacted> + 24
 libdispatch.dylib    <redacted> + 16
 libdispatch.dylib    <redacted> + 1068
 CoreFoundation       <redacted> + 12
 CoreFoundation       <redacted> + 1924
 CoreFoundation       CFRunLoopRunSpecific + 436
 GraphicsServices     GSEventRunModal + 104
 UIKitCore            UIApplicationMain + 212
 B4i Example          main + 104
 libdyld.dylib        <redacted> + 4
)
Application_Active
 

Attachments

  • SaveList.zip
    169 KB · Views: 192
Solution
I am trying to save a list with KeyValueStore but I am getting the following error:
The problem is, you want to fill the combobox with an empty list. Your KeyValueStore is empty for the "ListaElementos" key. Because you never add an item to it.
Just check if the ListStoreDate is empty, after you get the keyvaluestore.

This works:
B4X:
kvs.Initialize(xui.DefaultFolder, "store")
    
    ListStoreData.Initialize
    ListStoreData = kvs.Get("ListaElementos")
    
    If ListStoreData.IsInitialized = True Then
        B4XComboBox1.SetItems(ListStoreData)
    Else
        ListStoreData.Initialize
    End If
After that, you can add items to the store, with the Button1
B4X:
Sub Button1_Click...

Alexander Stolte

Expert
Licensed User
Longtime User
I am trying to save a list with KeyValueStore but I am getting the following error:
The problem is, you want to fill the combobox with an empty list. Your KeyValueStore is empty for the "ListaElementos" key. Because you never add an item to it.
Just check if the ListStoreDate is empty, after you get the keyvaluestore.

This works:
B4X:
kvs.Initialize(xui.DefaultFolder, "store")
    
    ListStoreData.Initialize
    ListStoreData = kvs.Get("ListaElementos")
    
    If ListStoreData.IsInitialized = True Then
        B4XComboBox1.SetItems(ListStoreData)
    Else
        ListStoreData.Initialize
    End If
After that, you can add items to the store, with the Button1
B4X:
Sub Button1_Click
    ListStoreData.Add(B4XFloatTextField1.Text)
    kvs.Put("ListaElementos", ListStoreData)
    B4XComboBox1.SetItems(ListStoreData)
    B4XFloatTextField1.Text = ""
    xui.MsgboxAsync("Saved", "B4X")
End Sub
if you now restart the app, you see, your new added item is now in the combobox, because the get request to the keyvaluestore is not empty for this key.
 

Attachments

  • SaveList.zip
    170 KB · Views: 179
Upvote 1
Solution
Top