NSTreeController how to save to file - file

Hi I am using an NSTreeController to control an NSOutlineView. This application loads bookmarks from file to application. As in the SourceView example in ADC:
http://developer.apple.com/mac/library/samplecode/SourceView/index.html
My questions is how do I save the bookmark to file once user makes the changes. Should I maintain the array/tree internally in my application and save before quitting or is there any easier methods?

You want to reverse the action taking place in the populateOutline method of MyWindowController.m. This method is reading the plist into one dictionary, reading a value from that dictionary, and using it to build the tree. Start with that method and follow the code to see how it is building the tree. It's using the BaseNode and ChildNode classes to build up the data model as a tree (I'm not sure why they didn't just use NSTreeNode). You want to reverse that procedure, ending up with an NSDictionary. You can then use writeToFile:atomically: to save the dictionary back to disk.
This can get as complex as you'd like to make it. For instance, the current code loads the dictionary file in a separate thread, so you could save in a separate thread, too. Or, you might want to save after every edit, again in a separate thread.

Related

Javascript - assign variable or array from outside file

This past year, I took a coding class on creating games using javascript. It is a basic game like Asteroids. When the game ends, it shows the top 5 high scores. The only problem is that when the program is restarted, the array that holds the scores is reset.
I want to store the high scores in a text file or spreadsheet. But, I cannot find a way to get my program to pull information from an outside file and assign it to either a variable, or I would rather put it into an array.
The second part is that when the game ends, it would need to send the updated array to the outside file if it is updated. Everything I look up involves HTML and CSS and we didn't learn this.
Is there any viable way to do this in Javascript?
It seems like the question is 'how do I read and write files from javascript'? The answer largely depends on where you're running the Javascript.
It doesn't sound like you're working in a browser, which makes it likely that you're using Node JS. If this is the case, you'll want to look at the File System API. Specifically, you'll want to create a filehandle by using fsPromises.open(). Once you have this reference to a location on your hard drive, you'll use filehandle.writeFile() to write a string directly to the file, and filehandle.readFile() to read a string directly from the same file.

How many Stateless Widget Classes should a Flutter Developer put in one file?

I'm currently working on a flutter application. I have a file with a big widget tree. In order to easier understand, read and maintain I wanted to "crop" the widget tree.
First what I did was to create multiple functions, which represented a bigger part of the tree such as _createFancyImage() or _createFancyContainer. After some research, I found out that such a design has some downsides (see: https://github.com/flutter/flutter/issues/19269). So then I decided to create StatelessWidgets instead. Because of the huge size of the widget tree, I broke it down to 3 logical StatelessWidgets. Now I can use FancyImage() or FancyContainer() which represent each a standalone widget.
As a beginner, I'm not sure whether I should keep those StatelessWidgetclasses within the same file. Alternatively, I could create independent files. One thing to clarify: I'm not using those fancy widgets somewhere else. Those are unique to this one big widget tree otherwise I could have outsourced them into a new folder such as "common_widgets" or "components".
Unfortunately, I couldn't find something within the Dart and Flutter Repo style guides nor on the internet.
I appreciate every suggestion.
You can add as much classes as you need in a single file. It depends on the developer's mindset.
But, let say if one of your class can be reused by other classes or packages then you should add it to another file for better separation.
I can favour you one approach is that your each file should have maximum one Stateful widget and as many Stateless widgets as you want for that corresponding widget will be a better scenario.
Still in some cases if you feel that more than enough stateless widgets has been added in a single file you should separate it in another file based on your choice.
I prefer to keep one public widget which having the same name as the filename and remaining private widgets.
comming to your ques is How many widgets in a single file?
Its actually depend there is no such rule to restrict the limit of file. Different authors having different preference. I prefer try to keep 5-6 classes(widgets) and
each one having 5-6 functions.
Try to make a file single responsible i.e(5-6 classes together responsible for single functionality). Don't make god class which having unrelated concerns together later it will pains(haha)
If it's a common widget keep them separate to respect DRY principle(Don't repeat yourself)
If the widget is further divided into 3-4 widgets or it children widget change depend upon rest response keep seprate for good practise
Bonus Tip: try using code folding shortcuts to push a little more

Flink reference data advice/best practice

Looking for some advice on where to store/access Flink reference data. Use case here is really simple - I have a single column text file with a list of countries. I am streaming twitter data and then matching the countries from the text file based on the (parsed) Location field of the tweet. In the IDE (Eclipse) its all good as I have a static ArrayList populated when the routine fires up via a static Build method in my Flink Mapper (ie implements Flinks MapFunction). This class is now inner static as it gets shirt on serialization otherwise. Point is, when the overridden map function is invoked at runtime from within the stream, the static array of country data is their waiting, fully populated and ready to be matched against. Works a charm. BUT, when deployed into a Flink cluster ( and it took me to hell and back last week to actually get the code to FIND the text file), the array is only populated as part of the Build method. When it comes to being used the data has mysteriously disappeared and I am left with an array size of 0. (ergo, not a lot of matches get found. Thus, 2 questions - why does it work in Eclipse and not on deploy (renders a lot of Eclipse unit tests pointless as well). Or possibly just more generally, what is the right way to cross reference this kind of static, fixed reference data within Flink? (and in a way that it is found in both Eclipse and the cluster...)
The standard way to handle static reference data is to load the data in the open method of a RichMapFunction or RichFlatMapFunction. Rich functions have open and close methods that are useful for creating and finalizing local state, and can access the runtime context.

Oozie: pass parameter via separate file

I'm experimenting with the loop 'hack' to iterate the same workflow over a list of of values and I am trying to load that list at run-time.
The list contains a number of files that I would like to process and so this list will change every time.
In the example the value is passed via the job.properties file and I can also pass it in the workflow itself as a key/value pair but than the values are hard-coded in my workflow and I would like to prevent that (as the list will change every time).
I found a similar question:
OOZIE: properties defined in file referenced in global job-xml not visible in workflow.xml
and the answer there explains also my initial mistakes (parameters vs properties) where I tried to pass it via a global job.xml file (which is only for properties and not for parameters) but a good solution is not provided.
One solution would be to read the parameter from a file via a shell action and capturing the output but I think there could be a more elegant way of reading this parameter in my workflow?

Initialize entire drive realtime model from string?

I want to serialize all of the data in my realtime document to a string with which I can later initialize a new realtime document. I want to do this so my users can make copies of their drive files, save different versions, etc, and I can re-initialize the relevant realtime documents from the string.
I see I can call document.getModel().getRoot().toString() to get a string representation of the root CollaborativeMap, but I don't see any easy way to load that string back IN to a CollaborativeMap. Also, the string returned is not JSON, so I can't easily use JSON.parse to turn it back into a normal JS object and iterate from there.
I can make this work by hand. Is there any easy automated way?
You can do that on the service side using the realtime.get() and update() methods:
https://developers.google.com/drive/v2/reference/realtime

Resources