TweetFollow Us on Twitter

June 96 - Newton Q & A: Ask the Llama

Newton Q & A: Ask the Llama

Q I have a slip in my application that edits part of my application preferences. I use GetAppPrefs to get the preferences frame, and then set a pointer to a subframe in my slip:

myAppPref.viewSetupFormScript := func()
begin
   local prefs := GetAppPrefs(kAppSymbol, kDefaultPrefFrame);
   self.target := prefs.defaultNames;
   inherited:?viewSetupFormScript();
end
The user makes the change and I use EntryChangeXmit, but sometimes I lose the change. Any hints?

A It looks as if you're encountering an interaction between soup entries and garbage collection. In your viewSetupFormScript, you use GetAppPrefs to load the soup entry corresponding to your application preferences into the NewtonScript heap. Then you set a target slot in your preferences slip to the defaultNames slot in that preferences frame. I assume that some time later, probably in the viewQuitScript, you reload your preferences frame (with GetAppPrefs again) and call EntryChangeXmit on the frame returned by that call.

The problem occurs because you use a local variable to point to your preferences entry. Once the viewSetupFormScript is completed, this local goes away, so the preferences entry is subject to garbage collection. This may seem unintuitive since soups are where you store persistent data. However, there's a difference between the data that comprises an entry in a soup and an actual entry in the NewtonScript heap. When you request a soup entry, the data from the soup is swapped into the heap so that you can access it as a frame. The entry on the heap is a copy of the data in the soup, not the real data. Changes to that heap copy aren't written to the soup until you call EntryChangeXmit.

After your call to GetAppPrefs, the heap looks like Figure 1. Then your viewSetupFormScript returns and the prefs local goes away, so your heap looks like Figure 2.

Figure 1. Heap after GetAppPrefs

Figure 2. Heap after viewSetupFormScript Notice in Figure 2 how there is nothing referencing the preferences entry, but there is something referencing the defaultNames subobject. As far as the system is concerned, the preferences entry frame is now available for garbage collection. The next time the preferences entry is loaded in, an entire new copy of that entry is made, including the defaultNames subobject. So self.target points to a valid NewtonScript array that's different from the new copy of your preferences entry.

This explains why the information doesn't get updated, but not why this doesn't happen every time. It doesn't happen every time because the soup system will cache the frame representation of a requested entry. When you request an entry, the first thing the soup system does is check for a cached entry. If it exists, it's used, in which case the defaultNames subobject is the same one that self.target is referencing -- that is, no new copy of the preferences entry is loaded into the heap.

So, what happens is that once the user finishes editing the entry, you call GetAppPrefs, which may return the cached preferences entry. If garbage collection has occurred, your target slot will point to the edited version of the defaultNames structure, but not to the defaultNames slot value from the new preferences frame. Figure 3 shows the heap after garbage collection has occurred. After your call to GetAppPrefs, you get the situation shown in Figure 4. Your local prefs variable points to a new heap copy of the preferences entry, but your target slot points to the old defaultNames value. The EntryChangeXmit call will affect the new copy of the soup entry, leaving it apparently unchanged.

Figure 3. Heap after garbage collection

Figure 4. Heap after GetAppPrefs There are two ways to fix this: you could put the edited defaultNames structure into the preferences frame before calling EntryChangeXmit, or you could hold a reference to the application preferences in your slip (or in your base view) for the duration of the edit. The first way is more memory efficient.

The lesson is that keeping around references to objects inside soup entries is a dangerous practice. The safe thing to do is read in your entry, do the modifications, save the entry, and set the reference to nil.

Q I have an application that may print or fax many pages of information. I need to draw a lot of the content of those pages. I know that in 1.x viewDrawScripts, faxing needed to be fast. How about in 2.0? Are there better ways to go?

A The main thing missing in Newton 1.x OS is a method that gets called before the fax connection is made. In Newton 2.0 OS, the formatInitScript method of your print format will be called before the connection is made. You can use this script to do time-consuming drawing and cache the results for later use.

An extension of this technique is to render all your pages into a Virtual Binary Object and then access the appropriate place in that object during printing or faxing. The advantage of this is that you save heap space, since a VBO is paged in to a system heap (not the NewtonScript heap). For some devices this is the only way to print large numbers of pages.

Q The setup application uses some nifty embedded keyboards. I checked the beta version of the Newton Programmer's Guide for a prototype, but there doesn't appear to be one. Is this an oversight? How can I make these keyboards?

A There are several, as yet undocumented, ROM prototypes for embedded keyboards. They will appear in the final Newton Programmer's Guide, but for now they're in the Newton Toolkit platform file:

  • protoAlphaKeys -- alphanumeric keypad

  • protoNumericKeys -- numeric keypad

  • protoTouchtonePad -- minimal phonepad

  • protoPhonePad -- phonepad plus punctuation and arrow keys
All of these embedded keyboards will send input to the current key view. All you have to do is draw one in your layout and make sure the target view is the current key view. See the Newton DTS Q&A document on the Newton Developer CD for instructions on how to use an afterScript to set the proto of a view.

Q I'm porting my code from Newton 1.x OS to Newton 2.0 OS. When I build my project using Newton Toolkit 1.6 and the 2.0 platform file, I get an error telling me that k<insertNameHere>Func is undefined. What's the problem?

A The chances are that your 1.x code is using one of the platform file functions that either have been incorporated into ROM or are obsolete. However, developers may want to write code that works on both Newton 2.0 and 1.x devices. To enable this, we provide the old functions but we mark them as deprecated, which means they shouldn't be used in Newton 2.0-savvy applications, but can be used for compatibility reasons.

For example, in 1.x platform files there's a kRegisterCardSoupFunc function; in the 2.0 platform file, this is called kRegisterCardSoupDeprecatedFunc since there's a new and better way to register soups in Newton 2.0 OS. See the platform file release notes for a list of deprecated functions, protos, and so on.

Q How are reals represented in the package format? The data field for 12.345 is represented as 0x4028B0A3D70A3D71, for example.

A NewtonScript uses the Apple SANE double format (basically the IEEE format) for floating-point numbers. These are implemented as 8-byte binary objects of class real and contain a sign bit, 11 bits of biased exponent, and 52 bits of fraction.

0x4028B0A3D70A3D71 is the binary data (8 bytes) of the SANE representation of 12.345. It's the same data that's used to hold the number on the Newton itself.

Q I would like to add a separator line followed by some new application-specific actions. I proceeded to register a frame with the title as the symbol pickSeparator. It worked, except that the separator was selectable. All I'm trying to achieve is an eye-pleasing separation between the system actions and my actions. I also tried returning in the GetTitle routine a frame with

{item: 'pickSeparator, pickable: nil} 
but that resulted in a blank entry. Is there a way to do what I'm trying to do -- that is, to have a pickSeparator that isn't selectable in the action button?

A In the routeScripts array, you can use a nil value instead of a frame. That will add another pickSeparator at that position in the routeScripts. Note that the system will fill in the separator between the items that are routing transports (like Print) and the items that are actions (such as Delete). If you need to add this separator dynamically, you can provide your own GetRouteScripts method that dynamically returns the routeScripts frame.

That said, please check the latest Newton 2.0 User Interface Guidelines to make sure that you're putting a separator in a valid spot.

Q I'm profiling my application to see why it takes so long to open. However, of the time it takes to open, only a small percentage is spent in my code. I'm measuring from the start of the viewSetupFormScript to the end of the viewSetupDoneScript in my base view.

A There are a few things you can do. The first is to make sure you're profiling system functions to see if that's where the time is going. It may be that you're doing things in your startup process that would be better done at a later time.

You may also be running into low-memory conditions. Run the HeapShow utility that comes with Newton Toolkit and look at the frames heap and free system space (handles and pointers). You can do this in combination with NS Debug Tools to step through your code and track memory usage. Note that the Newton Toolkit inspector will use a fair bit of system space, so you may want to get a baseline memory usage without the inspector connected.

Q I'm having two problems with a protoPicker view. First, when I open a protoPicker view (whose vFloating flag I haven't turned off) it's obscured by a textButton in the main view. I can't figure out why it doesn't float over this plain vanilla textButton.

Also, I can't select some of the items in the picker. The inaccessible items appear last in the list, from the portion of the picker view that extends beyond the picker's parent view or the slip's main view.

The only unusual thing I can see here is that the protoPicker view is not a sibling of the textButton. The view hierarchy looks like this:

slipMainView
   clusterView
   protoPicker
   textButton
Can you help?

A It looks like the protoPicker is being opened as a child of the clusterView. This means that the active (tappable) area of the protoPicker will be clipped to the bounds of the clusterView. It also sounds like the clipping viewFlag of the clusterView isn't set. That allows the protoPicker to be drawn outside of its parent, so you may think it's clickable even when it isn't.

There are three possible solutions:

  • Resize the protoPicker so that it's no larger than the clusterView.

  • Make the protoPicker a child of a view higher up in the hierarchy (for example, the slipMainView).

  • If your protoPicker is larger than the application base view, use BuildContext to attach it to the root view.
Q How do I reset a protoTextList so that when I change the listItems and redisplay, the display starts at the first item again? Right now if I've scrolled the text list and then I reset it, the top item is wrong.

A The documentation mentions a SetupList method that you call when you initialize the view. However, this is not enough if you're changing the listItems after you've opened the textList. Since the current implementation of textList scrolls by offsetting the origin, you also need to reset the origin.

Here's a method that you can add to your own protoTextList that will add a text item and redraw the list correctly:

myProtoTextList.AddItem := func()
begin
   // make sure listItems is an array
   if NOT listItems then
      listItems := [];

   local newItem := GetRandomWord(5, 10);
   // insert in sorted order for strings
   BInsert(listItems, newItem, '|Str<|, nil, nil);

   // redisplay based on new data
   // this will reset the list to the top item
   :SetOrigin(0, 0);
   :SetupList();
   :RedoChildren();
end;
Q Can you write a funny Q&A ?

A Yes.

The llama is the unofficial mascot of the Developer Technical Support group in Apple's Newton Systems Group. Send your Newton-related questions to dr.llama@applelink.apple.com (AppleLink DR.LLAMA). The first time we use a question from you, we'll send you a T-shirt.*

Thanks to Xopher Bell, Henry Cate, Bob Ebert, Mike Engber, David Fedor, Ryan Robertson, Jim Schram, Maurice Sharp, and Bruce Thompson for these answers. Special thanks to Bob Ebert for the answer on the format of reals.*

If you need more answers, check out http://dev.info.apple.com/newton on the World Wide Web or look at Newton Developer Info on Applelink.*


 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Seven Knights Idle Adventure drafts in a...
Seven Knights Idle Adventure is opening up more stages, passing the 15k mark, and players may find themselves in need of more help to clear these higher stages. Well, the cavalry has arrived with the introduction of the Legendary Hero Iris, as... | Read more »
AFK Arena celebrates five years of 100 m...
Lilith Games is quite the behemoth when it comes to mobile games, with Rise of Kingdom and Dislyte firmly planting them as a bit name. Also up there is AFK Arena, which is celebrating a double whammy of its 5th anniversary, as well as blazing past... | Read more »
Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »

Price Scanner via MacPrices.net

Apple introduces the new M4-powered 11-inch a...
Today, Apple revealed the new 2024 M4 iPad Pro series, boasting a surprisingly thin and light design that pushes the boundaries of portability and performance. Offered in silver and space black... Read more
Apple introduces the new 2024 11-inch and 13-...
Apple has unveiled the revamped 11-inch and brand-new 13-inch iPad Air models, upgraded with the M2 chip. Marking the first time it’s offered in two sizes, the 11-inch iPad Air retains its super-... Read more
Apple discontinues 9th-gen iPad, drops prices...
With today’s introduction of the new 2024 iPad Airs and iPad Pros, Apple has (finally) discontinued the older 9th-generation iPad with a home button. In response, they also dropped prices on 10th-... Read more
Apple AirPods on sale for record-low prices t...
Best Buy has Apple AirPods on sale for record-low prices today starting at only $79. Buy online and choose free shipping or free local store pickup (if available). Sale price for online orders only,... Read more
13-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices, along with Amazon’s, are the lowest currently available for new 13″... Read more
Amazon is offering a $100 discount on every 1...
Amazon has every configuration and color of Apple’s 13″ M3 MacBook Air on sale for $100 off MSRP, now starting at $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD): $999 $100 off... Read more
Sunday Sale: Take $150 off every 15-inch M3 M...
Amazon is now offering a $150 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 15″ M3 MacBook... Read more
Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more

Jobs Board

Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
LPN-Physician Office Nurse - Orthopedics- *Ap...
LPN-Physician Office Nurse - Orthopedics- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Supervisor/Therapist Rehabilitation Medicine...
Supervisor/Therapist Rehabilitation Medicine - Apple Hill (Outpatient Clinic) - Day Location: York Hospital, York, PA Schedule: Full Time Sign-On Bonus Eligible Read more
BBW Sales Support- *Apple* Blossom Mall - Ba...
BBW Sales Support- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 04388 Job Area: Store: Sales and Support Read more
BBW Supervisor- *Apple* Blossom Mall - Bath...
BBW Supervisor- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 0435C Job Area: Store: Management Employment Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.