A function returning file contents?

By the way, the wait for event block I suggested at the beginning is still event-driven. It simply allows the "when file1.GotText" block to remain within the function's scope. When it is encountered the system can process the event loop like it normally does. Upon triggering instead of jumping to just after the separate "when" block, execution would return to just after the wait block inside the function. It would require the context of the function to be saved at the time of the "wait" and restored before continuing with the function execution.

If you want to speed up, try the local sqlite database... see my extension here App Inventor Extensions: SQlite | Pura Vida Apps there are also other sqlite extensions...
See also a speed comparison between TinyDB and Sqlite in Q9 there

Generally see also


It is (Well, maybe a JavaScript can do it). You can write an App Inventor extension with the Rush IDE:

AI2 as it stands is broken. I don't believe all the detours suggested make even the slightest bit of sense. This gigantic hole needs to be fixed in its base. I think it is best to just try to forget that I ever looked into this mess.

I don't understand why we don't get a simple example (at least of the text file and the expected result) to see what it's all about. And please also post a small test aia.
I have to read here for a long time to guess what this topic could be about.

Note: Make it as easy as possible for those who are helping.
See also here.

1 Like

Please write a function to return the contents of a file as a list. It doesn't appear to be possible in AI2. See the picture I posted above (can't get much simpler than that). There is no way to get the result from the 'when' block back to the function for it to return it to the calling block without blocking the event loop. The code in the picture returns global filedata even if the 'when' block hasn't yet been activated to set it to the contents of the file. It doesn't allow the event loop to run and activate the 'when' block if the disabled block is enabled.

The suggestions I made in the second paragraph of the original post allow AI2 to be a useful "language". When I say 'wait' (or 'sleep') I don't mean an idle loop, but instead am intending for it to add an event to the queue with execution proceeding immediately after the 'wait' block (like the current AI2 event proceed to a point immediately following a 'when' block) upon the event firing. A tcl 'update' is somewhat similar as it defers execution back to the event loop and gets scheduled to resume when the event queue has been otherwise been emptied.

For those who think in terms of pictures of blocks, the language needs to add a 'when' block that has the shape of the 'set' blocks shown in the picture. I gave it a different name because it also indicates the blocks processing is complete until the event fires at some time in the future.

You could build your own extension to do this

1 Like

Why use AI2 if I am already working in a capable language?

I'm currently working on such an extension.

See this also:

Please show us your AIA or a test AIA and show the other blocks as well.

It is possible. I'll show the blocks a bit later.

Looking forward to seeing the blocks.

If that is the case, why are you asking this question in the first place !!

It was suggested that the solution to the problem was to write an extension in Java.

AI2 is written in blocky/javascript and java. Most if not all components are built using java.

Have a look at the sources....

I'm just saying if I have to write an extension in Java, then I might as well write the whole app in Java. I already have a complete flow chart.

As you found out yourself, reading a file works asynchronously... read the file and after a little bit of time depending on the size of the file you get the content of the file back in the event GotFile

If you think you have to duplicate blocks to get a result, you are doing something wrong... are you familiar with procedures? it looks like you first have to understand more about the architecture of App Inventor ... see again the links provided earlier in this thread by @ABG ...

And what about following the suggestion from @Anke

You said

This is not necessary, the only thing you have to do is to design your logic event orientated...



are you familiar with procedures?

I was asking for a function. Don't you think I might know about procedures?

The result is required inside a loop which is inside another loop. The logic is made manageable with the requested function. The only thing causing the problem is the fact that the file operation has been artificially split into two parts.

It is impossible to write code for a function returning the contents of a file? That is the complete nature of my question.

why the blocks had to be in one part if two parts works fine?
in app inventor async operation needs a separate event block to get the result, at least before anonymous procedure is implemented.

I found a survey article covering attempts to do stunts like this in various languages.

It's a fun read.
Don't miss the Pyramid of Doom.

1 Like

concurrent in a single-thread?