Working with Briefcases

February 12, 2013 in ROFX

bagageOne very useful feature of Data Abstract is Briefcase support. The Data Abstract briefcase provides local persistent storage. Simply store the data in a local briefcase file on the client file system, and the data can be reloaded without needing to request it again from the server.

Some common use cases:

  • Use briefcase for saving client data including uncommitted changes between restarts of your application.
  • Store some quite constant dictionary tables into briefcase and load them during application start and thus avoid having to reload the same data from the server.
  • Request only the changed records for some huge table from the server side and then merge the delta with the client table loaded from the local briefcase.
  • Use briefcases if you don’t have a persistent connection to your server and the user needs to be able to work in offline mode. Use Data Abstract’s robust conflict resolution to post the changes later.

In addition, a set of custom, application-specific string properties can be stored alongside the data. This can be useful, for example, for storing the version number of the data format to ensure newer versions of your application can reload the data correctly, if data formats change over time. (It is not recommended though to use this space for general application settings that are not directly related to the data.)

The main advantage of using a briefcase instead of some embedded database (f.e. SQLite.NET) to cache user data or to implement offline mode is that a briefcase stores not only user data but also its state (i.e. deleted, updated or inserted data rows). This greatly simplifies further synchronization with a Data Abstract-based server.

You can learn more about working with offline briefcases:

And more in the official RemObjects Documentation Wiki.

Avatar of marc

by marc

“Nougat”, Beta 3

February 11, 2013 in iOS, iPad, iPhone, Nougat, Oxygene, Visual Studio, WWDC

Beta 3

On Friday, we shipped BETA 3 of Oxygene “Nougat”, a major milestone in our progress to bring the Oxygene language you know and love to the “Cocoa” platform for truly native Mac and iOS development.

The big benefit of Oxygene “Nougat” over other non-Apple tool chains for Mac and iOS is that it fully embraces the platform and is a native compiler for the Objective-C runtime (which is at the core of Mac OS X and iOS development) that works directly with the native Cocoa API and controls.

This means you get all the benefits of the platform and you can work with and access all the same APIs that Objective-C developers using Xcode can. You can, for example, attend WWDC or NSConference or read and watch any of the trillion online tutorials out there on iOS (and Mac) development — and all the things you see and learn apply directly to the code you write with “Nougat”.

My friend and college Jim has created a great new video to introduce “Nougat” and show you around Beta 3, which you can find here.

Beta 3 is a huge milestone for us, in that we believe it represents a state in the development of the product where we can call it “usable for production work”. That doesn’t mean we’re close to RTM yet — we have big and strict plans for how solid we want the product to be for “1.0″ this summer — but it does mean that it is solid enough that you can start doing serious work with it. I should know, as i already have my first app created with Nougat in the App Store: Browse500. (Full source is on GitHub, too).

What’s Next?

As mentioned above, we still have a long way to go before RTM. Beta 1 last October had the goal of, well, getting something out to you guys to play with, and we got a lot of great feedback. For Beta 2 we focused on getting all the basic tool chain support in place (debugging, deploying, etc.) and our main focus for Beta 3, which we just shipped, was stability, so we spent most of our time fixing bugs in the compiler, IDE and toolchain. For Beta 4 we are shifting focus back on features — there are still quite a few things missing in the language, and we have lots of improvements and enhancements planned for the tool chain and the Visual Studio IDE support.

“Nougat” is not standing still, and the next two months should be a whirl-wind of new stuff going into the product.

After Beta 4 (and possibly a Beta 5), we are still on track for an official “1.0″ release (actually, it will be 6.0) in late spring/early summer.

Get Nougat!

How do you get “Nougat”? If you bought or renewed Oxygene from us since last October, you already have Oxygene “Nougat” as part of your product portfolio. Simply head over to beta.remobjects.com and get your copy of Beta 3 (and don’t forget to participate in the beta forums as well to let us know what you think).

(If you have a Suite Subscription for Xcode, that of course includes access to Oxygene “Nougat”, as well).

If you have a license for Oxygene for Java or for Embarcadero Prism XE2 or XE3, you can renew to the full suite of Oxygene for all three platforms at $349 in our secure online shop.

If you own Delphi XE2 or later, you can take advantage of our cross-grade offer for $399 to get the full Oxygene package.

And finally, the full Oxygene package is available for new users at $499.

Introducing “Nougat” – Oxygene for Cocoa

February 11, 2013 in tv

Introducing RemObjects “Nougat” – Oxygene for Cocoa. Bringing the Oxygene programming language to Cocoa and Cocoa Touch for Mac OS X and iOS development. Demonstration includes CrossBox, Interface Builder and Instruments integration.

Advanced XML

February 4, 2013 in tv

Oxygene for .NET makes it easy to work with XML documents. This video shows you how to use XML Namespaces, Schemas (XSD), XML Validation, Serialization, Deserialization, XPath and LINQ to XML.

Oxygene for Android Torch App

January 26, 2013 in Android, Cooper, Java, Oxygene

Brian Long made a great video on building an Android Torch App using Oxygene for Android. You can find it as part of RemObjects TV. The app he builds is a nice simple app, which makes the video easy to follow while also covering some concerns you may face in more complicated apps:

  • Permissions
  • Adding a new Activity
  • Using an Intent to move between activities
  • Creating a full screen Activity
  • Adding a Menu
  • Displaying Toast
  • Displaying a Dialog to get a user response

If you are new to Android Development with Oxygene, or maybe haven’t gotten into some of these topics yet then check out the video. You can also download the full source code for the app. Let us know what other Android related topics you would like to seen covered in future videos!

Building an Android Torch App

January 25, 2013 in tv

Brian Long walks through the process of developing an Android Torch app using Oxygene for Java. Covers Intents, Permissions, Activities, and other features common to Android App development. [Get the code.]

Oxygene Gets Big(Integers)

January 18, 2013 in .NET, Oxygene, Prism

A huge part of cryptography is big numbers. Really big numbers. For example, the SHA-256 cryptographic hash produces a 256-bit thumbprint of any data you give it. Exactly how big is 256-bits?

  • 8 bits = 256
  • 16 bits = 65536
  • 32 bits = 4294967296
  • 64 bits = 1.84467E+19
  • 128 bits = 3.40282E+38
  • 256 bits = 1.15792E+77

So the maximum value for a 256-bit unsigned integer is:

115 792 089 237 316 195 423 570 985 008 687 907 853 269 984 665 640 564 039 457 584 007 913 129 639 936

Unfortunately most programming languages only have 32-bit or at most 64-bit numbers available. So in order to store large numbers like these they require you to make the awkward move to byte arrays or some other complex construct. That isn’t the case with Oxygene.

Oxygene has the BigInteger datatype. BigInteger is an arbitrary length signed integer, so it can be as big as you have memory for, and it can be used like a normal integer.

To include BigInteger support you just need to add a reference to the System.Numerics.dll assembly (part of the .NET 4.0 Framework), add System.Numerics to your uses clause, and you are good to go. Now you can use BigIntegers just like any other data type.

Behind the scenes, BigInteger is actually an object with methods, properties, constructors and operators to make it easier to work with BigIntegers. So instead of using Math.Abs use BigInteger.Abs.

You are probably wondering “What does all of this have to do with cryptography?”. I’m glad you asked. Here is a method to calculate a SHA-256 on a string and return the value as a BigInteger

class method Hash.Sha256(text: String): BigInteger;
begin
  using hash256: SHA256Managed := new SHA256Managed() do begin
    var bytes := Encoding.UTF8.GetBytes(text);
    var hash := hash256.ComputeHash(bytes);
    exit new BigInteger(hash);
  end;
end;

The ComputeHash method on the SHA256Managed object returns a byte array containing the value of the hash. Luckily there is an optional constructor for BigInteger that takes a byte array as a parameter. This creates a BigInteger from the value of the byte array.

Another great place to use BigIntegers is with the cryptographic random number generator. Unlike the random number generator you are used to, the one provided by the RNGCryptoServiceProvider is cryptographically secure – meaning future values cannot be predicted. The RNGCryptoServiceProvider will generate a sequence of bytes of any size, which makes BigIntegers another great fit.

About creating a BigInteger from a byte array: Since a BigInteger is signed, this is just about as likely to result in a negative as a positive number. If you always want a positive number, you can append a zero byte to the end of the byte array, we will do that for our random number routine.

This method will return a BigInteger of the number of bytes specified.

class method Rnd.BigRnd(bytes: Cardinal): BigInteger;
begin
  using rng := new RNGCryptoServiceProvider do begin
    var tokens := new Byte[bytes + 1]; // one extra byte for the sign
    rng.GetBytes(tokens);
    tokens[bytes] := 0; // keep the result positive
    result := new BigInteger(tokens);
  end;
end;

Take a look at the code and see what sort of uses you can find for really big integers. BigInteger support was introduced a while ago for Oxygene for .NET – yet another feature you probably didn’t know you had.

Avatar of marc

by marc

Introducing Browse500, the first Nougat app on the iOS App Store!

January 17, 2013 in iOS, iPad, iPhone, Nougat, Oxygene, Photography

I’m more than thrilled to announce the immediate availability of Browse500.app, the very first app written in Oxygene “Nougat” to be submitted to and approved for the iOS App Store.

Dog-fooding is important, and as you know, we do a lot of it here at RemObjects. In early December i started out creating a small “real life” iOS app with the goal of (a) putting Nougat thru its paces, test it and find bugs, (b) create a nice sample application that illustrates Nougat and core iOS and Cocoa concepts in a real application and (c) “test” App Store submission.

I did not expect any problems with app store approval — after all, Nougat creates 100% native Cocoa apps that are virtually indistinguishable from those created with Xcode and Objective-C, but i wanted too “prove” that a Nougat app gets accepted (even with “Oxygene” in the compiler meta data, for example).

I was not disappointed. I submitted Browse500 on December 28th, the day the iTunes Connect (the place where you submit apps) was back from its holiday shutdown, and version 1.0 was approved just a few days later. I did not put 1.0 live in the store for two reasons: for one i’ve been working on the app and kept improving it while 1.0 was waiting for review, and for another, there were a few bugs and leaks that would kill the app after some period of browsing because it ran out of memory. Those were easily fixed thanks to Instruments. 1.0.2 was submitted last week and approved last night.

So what is Browse500?

 

From the user’s perspective, it’s a nice little app to browse the 500px photo community. It lets you browse popular and, as they call it, “fresh” photos, view individual photos full screen, and drill into individual user’s portfolios to see all their photos.

It’s simple, it’s quick, and it turns out i actually use it all the time myself to browse photos.

I’ll be expanding it over time; i have a “bookmark” feature in the works that’s not active in 1.0.2 yet (but that i’ve been using frequently), i have more navigation ideas, and i want to add support for logging authenticating (right now the app browses anonymously only), voting, favoriting, etc.

 

From the curious developer’s perspective, Browse500 is an open source app, [available on GitHub], written in 100% Oxygene Nougat that illustrates a wide range of iOS technologies and concepts, including:

  • The awesome and fluid kind of standard UI you get by using the proper iOS frameworks rather that weird hack frameworks that aren’t native
  • Use of UITableViews and iOS 6′s awesome new UICollectionView, which i use to implement the main “album” view of the app
  • Using custom UICollectionViewCells and custom UIViews
  • Working with both XIB-based and all-in-code views
  • Using Grand Central Dispatch to write asynchronous code that loads images and data from the net on demand and in the background — you’ll see that the album view is an endless stream of photos that just keeps filling with more as you scroll until 500px runs out of photos to show you
  • Using iCloud to sync settings (1.0.2 as one setting) and data (the 1.1 will have the bookmark feature alluded to above)
  • Using third party Objectve-C APIs (the app uses the open source PXAPI library to talk to 500px.com)
    … and much more

As mentioned, the app is on the App Store now, so if you are mainly interested in playing around, go get it (it’s free). And of course it’s a Universal app for iPhone and iPad.

The app is also fully open source. You can get the full source code from the GitHub repository (you will need the latest Nougat beta), and i would appreciate feedback, comments and &mdashl of course — pull requests.

Also, don’t forget to rate the app on the App Store!

 




(Disclaimer: The photos in this view are not by me.)

Avatar of marc

by marc

Using Instruments with Oxygene to Profile your Mac and iOS Apps

January 14, 2013 in iOS, Mac, Nougat, Oxygene, Visual Studio, Xcode

One of the coolest tools in Apple’s Xcode tool chain is Instruments, the profiler.

Profiling is an essential debugging tool for every developer, whether you want to tune the performance of a particularly time-sensitive piece of code, or drill into some memory issues (be it leaks or general memory load). With ARC, just like with Garbage Collection on .NET or Java, regular object leaks are rare, but one scenario where they can still happen (opposed to GC) is with so-called “retain cycles” — where object A holds on to object B, and vice versa.

Because Instruments is such an essential tool for the Cocoa developer, we have deeply integrated support for it into the Oxygene “Nougat” tool chain as well, and i’d like to demonstrate that in a quick (only somewhat contrived) sample.

Let’s say you have the following code:

type
  DummyData = class
  private
    fData: NSMutableArray;
  public
    method init: id; override;
    method Work; empty;
  end;
 
  DummyDataItem = class
  private
    fOwner: DummyData;
  public
    property owner: DummyData read fOwner;
    method initWithOwner(aOwner: DummyData): id;
  end;
 
implementation
 
method DummyData.init: id;
begin
  self := inherited init;
  if assigned(self) then begin
    fData := new NSMutableArray;
    for i: Int32 := 0 to 1000 do
      fData.addObject(new DummyDataItem withOwner(self));
leak
  end;
  result := self;
end;
 
method DummyDataItem.initWithOwner(aOwner: DummyData): id;
begin
  self := inherited init;
  if assigned(self) then begin
    fOwner := aOwner;
  end;
  result := self;
end;

Looks innocent enough. DummyData holds an array of DummyDataItems it initializes on creation; the code (naïvely) assumes the array and everything else to be released when the DummyData object itself goes out of scope.

Except it doesn’t, and your customer calls to complain that the app’s memory footprint is growing. How do you find out what’s going on? Instruments to the rescue.

In Oyxgene “Nougat” (as of BETA 3), Instruments is available right from inside Visual Studio. We’ve added a new menu item to the Debug menu (and you can also add it to the toolbar of course): Start With Instruments:

Hit that and Oxygene will build your app (if necessary), and via the magic of CrossBox, you’ll see Instruments popping up, Mac side — by default asking you what kind of analysis you want to perform:

Select Leaks and that will open an Instruments document, and also start your application running. Play around with the app and trigger the code paths that lead to the memory increase. In the Instruments window, you san see what’s happening, live — the overall memory load of the app keeps increasing (as shown in the Allocations instrument):

Quitting the app and selecting the Leaks instrument shows all the memory that was leaked — that is, not properly released. The picture is quite clear — it seems that 31 DummyData instances were created and never properly released. What’s up with that? After all, your code that creates DummyData is dead simple:

method MainWindowController.buttonClick(aSender: id);
begin
  var d := new DummyData;
  d.Work();
end;

“d” goes out of scope right after it’s used, and that should release the object, right?

Fold open one of the DummyData items in the list and click on the little arrow next to its address to drill into its retain/release history. You’ll see a huge list of roughly a thousand calls to retain. The call stack on the right tells you these happen from within “DummyDataItem.initWithOwner:”. That makes sense — your code creates a thousand of them, after all.

At the very end of the list, you see that from “buttonClick” your DummyData is being released though.

What’s going on? Shouldn’t “d” going out of scope release the array, which in turn releases the DummyDataItems, which in turn… wait, we’re getting close to the problem! It looks like our data structure contains what is called a “retain cycle”. The DummyData holds on to the NSArray, which holds on to the DummyDataItems which, in turn, hold on to the DummyData itself. Even though “d” is going out of scope, its retain count is only going down to 1001, because all the DummyDataItems still have references. As a result, the DummyData object actually never gets freed, and neither does the NSArray or the DummyDataItems inside it, which, in turn, can never give up their hold on the DummyData itself.

Though in this case we found the issue fairly quickly, Instruments has one more tool up its sleeve to make it even easier to find retain cycles: Click on the Leaks item in the navigation bar and select Cycles & Roots:

Instruments has actually detected any retain cycles for us and shows them in a list (in this case, 31 of the same), along with a nice graphical representation of what is going on.

From this view (even without our previous investigation), it becomes immediately clear that the fOwner reference from DummyDataItem back to DummyData is the culprit.

How do you break this vicious circle (assuming you cannot simply drop the owner reference altogether)? Weak references to the rescue!

type
  DummyDataItem = class
  private
    fOwner: weak DummyData;

By default, all variables and fields in Oxygene (and Objective-C with ARC) are strong — that means when an object is stored in the variable, its retain count is increased. By contrast, weak references just store the object, without affecting retain count. In fact, they do one better: they also keep track of the referenced object and automatically get set to nil when said object is released — so you never have to worry about the variable pointing to a stale object (which is a big problem in non-ARC languages).

Sidebar: A third type of object references are so-called unretained references. These behave like regular pointers in old-school languages; they store the object address, and when the object gets released that address will be stale — your code will be responsible for worrying about that.

With the code fixed, hit the Start With Instruments menu again. Your app will launch and Instruments will profile, and as you work with your app, you will notice that the memory load now stays down — as originally expected.

Of course, the Leaks pane will remain empty, but just to confirm, you can select the Allocations instrument, select Created & Destroyed in the sidebar and then locate and drill into one of the DummyData objects. As you can see, the retain/release history is much more sane now — no 1000 extra retains from DummyDataItem — and the object actually was released at the end of “buttonClick”.

Success!

 

So what have we seen in this article? We’ve had a quick look at how Instruments works and can be used to inspect memory allocations (the first phase of the investigation above does not just apply to bona-fide leaks and retain cycles, but can also be helpful if you just want to get a general impression of what memory your app is holding on to, and why), learned about retain cycles and strong, weak and unretained object references and we have also seen how Instruments can be used from Oxygene Nougat (which was so dead simple, it hardly warranted mentioning, wasn’t it?).

Cryptography with Oxygene

January 14, 2013 in .NET, Oxygene

One of my favorite development topics is Cryptography, and my favorite language is Oxygene. The Oxygene language has some great features that are well suited to advanced programming tasks like cryptography.

For example, it makes it very easy to work with the AesManaged class in the .NET Framework. When you construct an instance of the AesManaged class, it initializes both the Key and the initialization vector (IV) with new random values. Tuples are a great option for returning both of these values from a single method.

class method OxygeneCrypto1.GenerateKeys: tuple of (AESBlock, AESKey);
begin
  using aes := AesFactory do
    exit(aes.IV, aes.Key);
end;

When calling this method, you just use the syntax (IV, Key) := GenerateKeys; and the two local variables receive the values from the Tuple. This is much better than using out functions or creating a custom object just to hold the two values.

Oxygene’s awesomeness doesn’t stop there. Here is a method to encrypt a string with AES and the provided key. It first compresses the string – this strengthens the encryption and reduces the size of the ciphertext. Input parameters are validated with require statements, and ensure statements are used to verify it worked correctly. Nested using statements manage the life-cycle of all the handles. The result is much more concise code that is clearer and easier to maintain than the alternative in other languages.

class method OxygeneCrypto1.encrypt(plainText: String; Key: AESKey; IV: AESBlock): array of Byte;
require
  assigned(plainText) : 'The method encrypt requires the plainText parameter to be defined.';
  plainText.Length > 0 : 'The method encrypt requires the plainText parameter to be defined.';
  assigned(Key) : 'The method encrypt requires the Key parameter to be defined.';
  assigned(IV) : 'The method encrypt requires the IV parameter to be defined.';
  Key.Length = AESKeyLength : 'Invalid Key Length when calling encrypt.';
  IV.Length = AESBlockLength : 'Invalid IV Length when calling encrypt.';
begin
  using aesAlg := AesFactory(Key, IV) do
  using encryptor: ICryptoTransform := aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV) do
  using output := new MemoryStream() do
  using encStrm := new CryptoStream(output, encryptor, CryptoStreamMode.Write) do
  using gzip := new DeflateStream(encStrm, CompressionLevel.Fastest) do
  using writer := new StreamWriter(gzip) do begin
    writer.Write(plainText);
 
    // Close writer to prepare the output
    writer.Close();
 
    exit output.ToArray();
  end; 
ensure
  assigned(result) : 'Undefined result from encrypt.';
  result.Length > 0 : 'Undefined result from encrypt.';
end;

The decryption method is just as clear and concise. Take a look at the example download to see how it all fits together.

I hope to make this a series of posts on different cryptography solutions with the different platforms each flavor of the Oxygene language supports. So let me know if you find Cryptography as interesting as I do. There are so many other topics to cover, too!