beautypg.com

Apple Newton Programmer’s Newton 2.0 (for Newton 2.0) User Manual

Page 493

background image

C H A P T E R 1 1

Data Storage and Retrieval

Using Newton Data Storage Objects

11-61

Copying Entries

11

The

EntryCopyXmit

global function and the

CopyEntriesXmit

soup method

enable you to copy entries from one soup to another and transmit appropriate
change notifications.

The following code fragment uses the

CopyEntriesXmit

soup method to copy

all the entries from a specified source soup into a specified destination soup. Note
that this method is defined only for soups, not for union soups. The following code
fragment uses the

GetMember

union soup method to retrieve the plain soup

constituent of a union soup from a specified store. The

GetMember

method never

returns

nil

; instead, it creates an empty member soup on the specified store if one

does not already exist:

// myUsoup member on internal store is the source soup

local myUSoup := GetUnionSoupAlways("myUSoup:mySig");

local sourceSoup := myUSoup:GetMember(GetStores()[0])

// myUsoup member on another store is the destination soup

local destSoup := myUSoup:GetMember(GetStores()[1]);

// copy all entries from source soup to dest soup

local cursor := sourceSoup:Query(nil);

if (cursor:CountEntries() <> 0) then

sourceSoup:CopyEntriesXmit(destSoup, '|MyApp:MySig|);

You can use the

EntryCopyXmit

function to copy an entry from a specified

source soup to a specified destination soup and transmit a soup change notification
message. Note that this function is defined only for soups, not for union soups. The
following code fragment uses the

GetSoup

store method to retrieve a specified

soup from its store. Because the

GetSoup

method returns

nil

when the soup to

be retrieved is not available, you must at least ensure that this result is non-

nil

before using it. The following code fragment actually goes one step further and
uses the

IsValid

soup method to test the validity of the

destSoup

soup in

additional ways:

local myUSoup := GetUnionSoupAlways("myUSoup:mySig");

// get all entries having 'hot in 'temperature slot

local cursor := myUSoup:Query({indexPath: 'temperature,

beginKey: 'hot

endKey: 'hot});

local destSoup := GetStores()[0]:GetSoup("mySoup:mySig");

// make sure we actually got a valid soup

if destSoup:IsValid() then

begin

// xmit a single notification after all changes are made

while e := cursor:Entry() do EntryCopyXmit(e,destSoup,nil);

XmitSoupChange(destSoup, '|MyApp:MySig|, 'whatThe, nil);

end;